The jQuery Validate plugin is a jQuery plugin by Jörn Zaefferer. Its purpose is to perform client-side form validation of user entered data.
The jQuery Validate plugin is a jquery plugin by Jörn Zaefferer. Its purpose is to perform client-side form validation of user entered data.
Helpful Links:
Stack Snippet Starter Pack:
HTML - Include the plugin script someplace after the jQuery library:
(Use CDN links or host the scripts yourself)
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.14.0/jquery.validate.js"></script>
<form id="myform" action="post.php">
<input type="text" name="first_name" /><br/>
<input type="text" name="last_name" /><br/>
<input type="text" name="phone" /><br/>
<input type="submit" />
</form>
$(document).ready(function() { // <-- ensure form's HTML is ready
$("#myform").validate({ // <-- initialize plugin on the form.
// your rules and other options,
rules: {
first_name: { // <-- this is the name attribute, NOT id
required: true
},
last_name: {
required: true
},
phone: {
required: true,
digits: true
}
}
});
});
jsFiddle Demo: http://jsfiddle.net/2nhcfkLj/
Documented Options: http://jqueryvalidation.org/validate
Helpful questions:
- How to create a simple, custom rule
- How to declare rules
- Changing default error messages
- Assign rules without using
nameattribute - How to validate a hidden field
- How to use jQuery Validate in ASP.Net with Master Pages
Other typical issues:
- All input elements to be validated must be enclosed within a set of
<form></form>tags. The only elements that can be validated areselect,textarea, certaininputtypes, and certain elements containing thecontenteditableattribute. - Rules are defined by input
nameattributes, not byid, when declared within therulesoption of.validate(). - all input elements to be validated must contain a unique
nameattribute. (All radio or checkbox elements within a single "grouping" may share the samenameas this one grouping is considered a single data point. However, each grouping must contain a uniquename.) .validate()should be called once within DOM ready to initialize the plugin. Optionally use.valid()to test the form for validity and get a boolean result of that test.- There is no need to enclose
.validate()inside of anyclickorsubmithandler. The plugin will automatically capture and handle the submit button. - A
namewith certain special characters must be enclosed in quotes when declared within therulesoption of.validate(). - Use the
submitHandlercallback function to deal with successfully validated forms and/or submit viaajax. - Use the
invalidHandlercallback function for invalid forms. - If using the
highlightorunhighlightcallback function, be sure to also include the other one. They are complementary and should be used together for best results. - By default, the plugin will ignore any hidden input elements. This can be prevented by setting the
ignoreoption toignore: [](ignore nothing; validate everything). - If you have multiple
submitbuttons where one (such as "save") needs to bypass validation but still needs to submit the form data, useclass="cancel"on the button.