You can simply use the jQuery Validate plugin as follows.
jQuery:
$(document).ready(function () { $('#myform').validate({ // initialize the plugin rules: { field1: { required: true, email: true }, field2: { required: true, minlength: 5 } } }); });
HTML:
<form id="myform"> <input type="text" name="field1" /> <input type="text" name="field2" /> <input type="submit" /> </form>
DEMO: http://jsfiddle.net/xs5vrrso/
Options: http://jqueryvalidation.org/validate
Methods: http://jqueryvalidation.org/category/plugin/
Standard Rules: http://jqueryvalidation.org/category/methods/
Optional Rules available with the additional-methods.js file:
additional-methods.js
maxWords minWords rangeWords letterswithbasicpunc alphanumeric lettersonly nowhitespace ziprange zipcodeUS integer vinUS dateITA dateNL time time12h phoneUS phoneUK mobileUK phonesUK postcodeUK strippedminlength email2 (optional TLD) url2 (optional TLD) creditcardtypes ipv4 ipv6 pattern require_from_group skip_or_fill_minimum accept extension
1.4m articles
1.4m replys
5 comments
56.9k users