1. What is it ?

Generic JavaScript validator 1.0 (beta) is a simple but powerful cross-browser JavaScript that combines DOM and regulars expressions for the client-side validation of forms. It can validate any number of form on HTML documents. It also supports user defined JavaScript functions.


2. Include .js file
Inside <head> section of the html with the form inside you insert these lines...

<script language="JavaScript" type="text/javascript" src="validate.js">
</script>

3. Attribute List
Attribute Allowed values Behavior
required [yes | no] if set to "yes" this field can't be left empty (you must insert at least one char). If set to "no" the field becomes optional, but if a value is inserted, it must match with a pattern.
authtype _specialchars No Special characters allowed, but numbers are allowed.
_specialcharsandnumbers No Special characters and numbers are allowed.
_digits Only decimal values are allowed.
_email Checks for valid with standard Email regular expression.
_url Checks for valid website address. Website address like http://www.www.www or www.www.www are allowed.
_date Date format DD-MM-YYYY, DD/MM/YYYY and DD.MM.YYYY are allowed. Also checks for leap year and invalid months as well as days.
_phonenumber Checks for Digits only.
_mobilenumber Match for mobile number pattern (+xx)xxxxxxxxxx or xxxxxxxxxx
_usphonenumber Match for standard US phone number pattern (xxx)xxx-xxxx
_zip Checks for Digits only (must be of 5 or 6 digits length).
Its also allows U.K. postcode
_username Checks for username pattern.
  • No special characters allowed at starting of username.
  • (.-_) dot,dash,underscore may come in between a username.
  • End character of username may contain alpha-numeric values.
_flname Checks for First/Last Name pattern. No special characters allowed in First/Last name.
_currency Checks for decimal or float values.
  _custom Any pattern, which you are going to specify in to pattern attribute.
errormsg Error message Specify, your own error message alert.
pattern Your own Regular Expression or Pattern for comparison with user input. Specify, your own pattern to be validate with an input field.

4. Some Examples

Zip/Pincode Validation
Indian Postal code(395001) - 6 digits allowed.
Pincode : Show Code 
Date Validation
DD-MM-YYYY or DD/MM/YYYY or DD.MM.YYYY
Please enter the date : Show Code 
Custom Pattern
Allowd only Jpg files
Only Jpg files : Show Code 
  Click here to browse example page.

5. Your own functions
Amount must be greater or equal to 100.
Your own function
Enter amount : Show Code 

6. Adding your own functions.
This validator is purely based on regular expression (i.e. pattern) matching.
to add your own functions in validate.js, please follow the coding structure as follow...


function _yourfunction()
{
      if( fieldValue !="") // Checks for blank field.
      {
      var regex = /^ YOUR REGULAR EXPRESSION $/;
      if(!regex.test( fieldValue ))
      {
           if(! fieldMsg ){
           alert(" Your Error Message ");
           }else{
           alert( fieldMsg );
// Alerts user's error message provided by errormsg attribute.
           }
      __setFocus();
// Will focus the field,on which validation got failed.
      return false; // Extremely necessary to return true or false.
      }
      }
      return true; // Extremely necessary to return true or false.
}

7. Leave your feedback.
Please drop your comments/suggestions/bugs here...
Your name :
Your Email :
Comments :