misc validation ------------------------- Valid Date Format A valid short date should consist of a 2-digit month, date separator, 2-digit day, date separator, and a 4-digit year (e.g. 02/02/2000). It would be nice to allow the user to use any valid date separator character that your backend database supported such as slashes, dashes and periods. You want to be sure the user enters the same date separator character for all occurrences. This can be done with a regular expression like this: var objRegExp = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/ -------------------------------------- Table: The Validation Descriptors maxlen=??? maxlength=??? Check whether the length of the data exceeds the allowed maximum. For example, if the maximum size permitted is 25, give the validation descriptor as "maxlen=25" minlen=??? minlength=??? Check the length of the entered data with the required minimum. Example "minlen=5" required req Raise error if the field is left empty. email The field is an email field and verify the validity of the data. alphanumeric alnum Check the data if it contains any characters other than alpahbetic or numeric characters num numeric Check numeric data alpha alphabetic Check alphabetic data. lt=??? lessthan=??? Verify the data to be less than the value passed. Valid only for numeric fields. Example: if the value should be less than 1000, give validation descriptor as "lt=1000" gt=??? greaterthan=??? Verify the data to be greater than the value passed. Valid only for numeric fields. Example: if the value should be greater than 10, give validation descriptor as "gt=10" regexp=??? Check with a regular expression; the data entered should match the regular expression. Example: "regexp=^[A-Za-z]{1,20}$" allow up to 20 alphabetic characters. **** remember to put \ before $ and other \ and other strange characters. dontselect=?? This validation descriptor is valid only for
Password :
and that’s it! Notice that gen_validation.js is the file which contains the source code for the form validation function validateForm() the contents of this file is given in page 3. When the user presses the submit button, the onsubmit event will be triggered, which will call the validation function, passing the form object and the validation descriptions. If the user enters invalid data, or if the required fields are empty, an error message will be displayed and the validation function will return false. This will prevent the form from being submitted. But, the Error message may not be descriptive enough as you like it to be. If the user didn't enter the login name, for example, the error message displayed will be "LoginName: Required field." That too, if the name of the input text element is given as "LoginName" It is possible to give your own error messages. The example below shows the code modified to contain the custom error messages The rest is the same. In general, the format of the validation description is: arrayname= [ [ // Validations for input item 1 [ , ] [ , ] ...... ...... ], [ // Validations for input item 2 [ , ] [ , ] ...... ...... ] ................. ................. ]; As you saw, the is optional. It is important to keep in mind that the order of the input items in the array and the order in the form should be the same. What if a particular field doesn't have any validations? Just add a null record like this: The validation descriptor records should be in the order of the corresponding fields in the form. It may be difficult to keep track of the order in large forms. So, here is an easier way. Declare the array before the form tag. Add the descriptors for each input field just under the input field. Like this:
LoginName: Password :