Using client side JavaScript is an efficient way to validate the user input in web forms. When there are many fields in the form, the JavaScript validation becomes too complex.
The JavaScript class presented here makes the form validations many times easier.
1.Download script: http://www.javascript-coder.com/html-form/javascript_form.zip and include gen_validatorv31.js in your html file just before closing the HEAD tag
<script language="JavaScript" src="gen_validatorv31.js" type="text/javascript"></script> </head>
2. Just after defining your form, Create a form validator object passing the name of the form
<FORM name='myform' action=""> <!----Your input fields go here --> </FORM> <SCRIPT language="JavaScript"> var frmvalidator = new Validator("myform"); ....
3. Now add the validations required
frmvalidator.addValidation("FirstName","alpha");
the first argument is the name of the field and the second argument is the validation descriptor, which specifies the type of validation to be performed.
You can add any number of validations. The list of validation descriptors are provided at the end of the documentation.
The optional third argument is the error string to be displayed if the validation fails.
frmvalidator.addValidation("FirstName","alpha"); frmvalidator.addValidation("FirstName","req","Please enter your First Name"); frmvalidator.addValidation("FirstName","maxlen=20", "Max length for FirstName is 20");
4. Similarly, add validations for the fields where validation is required.
That's it! You are ready to go.
1 year 19 weeks ago