1.Include gen_validatorv2.js in your html file just before closing the HEAD tag
<script language="JavaScript" src="gen_validatorv2.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.
Example
The example below will make the idea clearer
<form action="" name="myform" >
<table cellspacing="2" cellpadding="2" border="0">
<tr>
<td align="right">First Name</td>
<td><input type="text" name="FirstName"></td>
</tr>
<tr>
<td align="right">Last Name</td>
<td><input type="text" name="LastName"></td>
</tr>
<tr>
<td align="right">EMail</td>
<td><input type="text" name="Email"></td>
</tr>
<tr>
<td align="right">Phone</td>
<td><input type="text" name="Phone"></td>
</tr>
<tr>
<td align="right">Address</td>
<td><textarea cols="20" rows="5" name="Address"></textarea></td>
</tr>
<tr>
<td align="right">Country</td>
<td>
<SELECT name="Country">
<option value="" selected>[choose yours]
<option value="008">Albania
<option value="012">Algeria
<option value="016">American Samoa
<option value="020">Andorra
<option value="024">Angola
<option value="660">Anguilla
<option value="010">Antarctica
<option value="028">Antigua And Barbuda
<option value="032">Argentina
<option value="051">Armenia
<option value="533">Aruba
</SELECT>
</td>
</tr>
<tr>
<td align="right"></td>
<td><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
<script language="JavaScript" type="text/javascript">
var frmvalidator = new Validator("myform");
frmvalidator.addValidation("FirstName","req","Please enter your First Name");
frmvalidator.addValidation("FirstName","maxlen=20",
"Max length for FirstName is 20");
frmvalidator.addValidation("FirstName","alpha");
frmvalidator.addValidation(“LastName”,”req”);
frmvalidator.addValidation(“LastName”,”maxlen=20″);
frmvalidator.addValidation(“Email”,”maxlen=50″);
frmvalidator.addValidation(“Email”,”req”);
frmvalidator.addValidation(“Email”,”email”);
frmvalidator.addValidation(“Phone”,”maxlen=50″);
frmvalidator.addValidation(“Phone”,”numeric”);
frmvalidator.addValidation(“Address”,”maxlen=50″);
frmvalidator.addValidation(“Country”,”dontselect=0″);
</script>
Some Additional Notes
Adding Custom ValidationIf you want to add a custom validation, which is not provided by the validation descriptors, you can do so. Here are the steps: function DoCustomValidation() frmvalidator.setAddnlValidationFunction("DoCustomValidation"); The custom validation function will be called automatically after other validations. If you want to do more than one custom validations, you can do all those validations in the same function.
Clear All ValidationsIn some dynamically programmed pages, it may be required to change the validations in the form at run time. For such cases, a function is included which clears all validations in the validator object.
this function call clears all validations you set. Table of Validation Descriptors
<!– 21 Oct 2006 Blocking link to simfatic.com
Example Page |