Helping ordinary people create extraordinary websites!
Scripts Directory JavaScript Scripts Forms

Form Field Validation

This series of functions provide common form field validation.You customize the "validateForm" function to make calls to the following functions with your form fields.If the validation fails, the functions will alert the user with a message and put the text input focus on the field with the error.



validRequired(formField, fieldLabel) -- checks for a field being empty

validEmail(formField, fieldLabel, required) -- checks for a correctly formatted email address

validNum(formField, fieldLabel, required) -- checks for a valid number (a string where all the characters are digits)

validInt(formField, fieldLabel, required) -- checks for a number (integer) that can be correctly parsed with the function parseInt

validDate(formField, fieldLabel, required) -- checks for a correctly formatted date


The parameters to these functions are defined as:

formField -- the document reference to your form field (e.g., myform.fieldName)

fieldLabel -- the user friendly string that you want the error message to refer to your field by

required -- whether the field is required to be filled in by the user (true/false)

To add to your form, add the onsubmit handler to the form tag as shown in the example code.


Author: Robert Dominy   Version: n/a
Price: Free   Demo: View it
Platform(s): IE 3.0+, NS 3.0+   Download Script: Click here
 12 Votes

More JavaScript Forms Scripts: