Javascript Form Validation: Quick and Easy!
Javascript Form Validation: Quick and Easy!
≡ Menu
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.
Contents
1. Download the JavaScript form validation script
javascript-coder.com/html-form/javascript-form-validation.phtml 1/14
7/16/2019 JavaScript Form Validation : quick and easy!
The script has a catalog of almost all the
common validation types built-in.
In other words, in order to validate a field, you just associate a set of validation descriptors
for each input field in the form.
2. Just after defining your form, create a Validator() object passing the name of the form
See below for the complete list of validation descriptors. The third parameter ( Error
string ) is optional.
You can add any number of validations to a field.
Example
Here is a complete example:
<script 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("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=000");
</script>
Your form should have a distinguished name. If there are more than one form in the
same page, you can add validators for each of them. The names of the forms and the
validators should not clash.
javascript-coder.com/html-form/javascript-form-validation.phtml 4/14
7/16/2019 JavaScript Form Validation : quick and easy!
You can’t use the javascript onsubmit event of the form if it you are using this validator
script. It is because the validator script automatically overrides the onsubmit event. If
you want to add a custom validation, see the section below
1. Create a javascript function which returns true or false depending on the validation
function DoCustomValidation()
{
var frm = document.forms["myform"];
if(frm.pwd1.value != frm.pwd2.value)
{
sfm_show_error_msg('The Password and verified password does not
match!',frm.pwd1);
return false;
}
else
{
return true;
}
}
sfm_show_error_msg() function displays the error message in your chosen style. The
first parameter is the error message and the second parameter is the input object.
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.
function DoCustomValidation()
{
var frm = document.forms["myform"];
if(false == DoMyValidationOne())
{
sfm_show_error_msg('Validation One Failed!');
return false;
}
else
javascript-coder.com/html-form/javascript-form-validation.phtml 5/14
7/16/2019 if(false == DoMyValidationTwo())
JavaScript Form Validation : quick and easy!
{
sfm_show_error_msg('Validation Two Failed!');
return false;
}
else
{
return true;
}
}
frmvalidator.clearAllValidations();
frmvalidator.EnableFocusOnError(false);
dontselect=?? “dontselect=000”
javascript-coder.com/html-form/javascript-form-validation.phtml 7/14
7/16/2019 JavaScript Form Validation : quick and easy!
Validation DescriptorUsage
This validation descriptor is only for check boxes. The user should not select
dontselectchk=?? the given check box. Provide the value of the check box instead of ??
For example, dontselectchk=on
This validation descriptor is only for check boxes. The user should select the
shouldselchk=?? given check box. Provide the value of the check box instead of ??
For example, shouldselchk=on
One of the radio buttons should be selected.
Example:
selone_radio
chktestValidator.addValidation("Options","selone");
eqelmnt=??? Example:
frmvalidator.addValidation("confpassword","eqelmnt=password",
"The confirmed password is not same as password");
The input should be less than the other input. Give the name of the other
ltelmnt=???
input instead of ???
The input should be less than or equal to the other input. Give the name of
leelmnt=???
the other input instead of ???
The input should be greater than the other input. Give the name of the other
gtelmnt=???
input instead of ???
The input should be greater than or equal to the other input. Give the name
geelmnt=???
of the other input instead of ???
Go to the second part of this post to learn about the advanced features of this
validation script.
Learn More
Javascript Form validation Video Tutorial
javascript-coder.com/html-form/javascript-form-validation.phtml 8/14
7/16/2019 JavaScript Form Validation : quick and easy!
Related posts:
Previous Comments
bks
i have solved it look like submit button should have name=”submit” and id=”submit”
other then that will not submitting value;
bks
i am using this validator in php form, Problem passing all validation but values are not
“post”
Jim
Almost working perfectly. Cannot for the life of me radio buttons to validate. No error but no
validation when a radio button has to be selected. Any ideas? BTW the demo for radio
buttons doesn’t work for me either.
Jim
javascript-coder.com/html-form/javascript-form-validation.phtml 10/14
7/16/2019 JavaScript Form Validation : quick and easy!
Don’t worry, worked it out. Had to use frmvalidator instead of chktestValidator!
Evan
I’ve tried your validator and it seems to try and validate when the page loads and the input
fields are empty. I get an error from the frmvalidator.addValidation() for any fields that do
not have default values. Am I doing something incorrectly?
Previous Comments
javascript-coder.com/html-form/javascript-form-validation.phtml 11/14
7/16/2019 JavaScript Form Validation : quick and easy!
Categories
Form Handling
Form Validation
HTML Forms
JavaScript Button
jQuery
Tutorials
javascript-coder.com/html-form/javascript-form-validation.phtml 12/14
7/16/2019 JavaScript Form Validation : quick and easy!
javascript-coder.com/html-form/javascript-form-validation.phtml 13/14
7/16/2019 JavaScript Form Validation : quick and easy!
javascript-coder.com/html-form/javascript-form-validation.phtml 14/14