1 / 0

CSCI 6962: Server-side Design and Programming

CSCI 6962: Server-side Design and Programming. Input Validation and Error Handling . Form Validation. Detecting user error Invalid form information Inconsistencies of forms to other entities Enter ID not in database, etc. Correcting user error

ismail
Télécharger la présentation

CSCI 6962: Server-side Design and Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSCI 6962: Server-side Design and Programming

    Input Validation and Error Handling
  2. Form Validation Detecting user error Invalid form information Inconsistencies of forms to other entities Enter ID not in database, etc. Correcting user error Providing information or how to correct error Reducing user memory load Preventing user error Good instructions Field types/values that prevent error Error tolerance Example: Accepting phone numbers in multiple formats
  3. Example ValidationBean public String validate() { // Validate form elements // Return “valid” if all valid // Return “invalid” otherwise // and return to page
  4. What to Validate Required fields have input Text inputs non-empty Trim method useful to remove leading, training spacesname = name.trim();if (name.equals(“”)) { … Radio button groups and other lists have selection where required
  5. Error Prevention Tell user what is required, optional Set default valueswhere appropriate by settinginitial values
  6. Validating Numeric Inputs What if user enters non-numeric value? intquantNum = Integer.parseInt(quantity); Exception thrown in Java Cannot parse “five” “five” ValidateBean validate method Integer class parseIntmethod NumberFormatException thrown
  7. Validating Numeric Inputs Unhandled exceptions cause error screen Must handle with try/catch blocktry {code which might cause exception …} catch (ExceptionTypevariable) {code to handle exception }code after block Jump here if exception Skip if noexception Set return value to forward to original or error page
  8. Validating Numeric Inputs Jump here if NumberFormatexception due to quantity not being a number faces-config will forward to original page Skip if noexception
  9. Numeric Error Prevention Avoid direct numeric input if possible Provide dropdowns that list values if possible Can use loop to generate array of SelectItemobjects
  10. Validating Input Is numeric input valid? Negative quantity invalid What about quantity of 0? Is combination of choices legal? Is format of input legal? Credit card number 16 digits Phone number in correct format
  11. Error Prevention Tell user if format or other rules apply
  12. Regular Expressions Tool for verifying an input string is in a given format Easier than parsing it yourself! Examples: Credit card contains 16 digits Phone number in form (3 digits) 3 digits - 4 digts Email in form characters@characters.characters Note that correct format  legal Nonexistent phone number, etc. Will need to verify against database
  13. Regular Expressions Matching single characters
  14. Regular Expressions Metacharacters match characters of a certain type Note: the extra “\” in front is required by Java
  15. Regular Expressions Combining regular expressions Quantifiers give number of times a char must appear
  16. Regular Expressions Examples: Credit card number:\\d{16} Phone number: \\d{3}-\\d{3}-\\d{4} Email address: \\w+@\\w+(\.\\w+)*
  17. Regular Expressions Java syntax: String.match(“regularexpression”) Returns true if Stringis in form regularexpression
  18. Error Tolerance Don’t reject based on format if any chance input valid Example: other legal phone numbers 555-555-5555 (555) 555-5555 555.555.5555 … Choose most tolerant pattern to prevent false rejection “Phone number is 10 digits separated by any number of non-digits” Pattern: (\\d\\D*){10} digit Any number of non-digits 10 times
  19. Calendar Dates in Java Construct a new GregorianCalendarobject Contains information about current date when created Must import java.util.* library Useget(Calendar.fieldname)method to get component of that date Field names = YEAR, MONTH, etc. Returns an integer
  20. Calendar Dates in Java Can use to generate values from current date Get current year Generate new SelectItemfor each of the next 10 years
  21. Calendar Dates in Java Can validate things about dates entered by user Caution: Date for user may be different from server Inaccurate clocks, international date boundary Safest to only use for month, year.
More Related