Import JS file
After downloading the package validate file is located in the Dist directory, the directory contains Jquery.validate.js and additional-methods.js as well as their respective min files. Additional-methods.js is an additional authentication method that can be imported as appropriate.
<script src= "./js/jquery.js" type= "Text/javascript" ></script>
<script src= "./js/ Jquery.validate.js "type=" Text/javascript "></script>
<script src="./js/additional-methods.js " Type= "Text/javascript" ></script>
How to use
Take the registration page as an example, you need to verify the user name, password, duplicate password, and authentication code.
Where the user name needs to detect uniqueness, the verification code needs to detect whether it is correct. Form-register is the form ID, and the validation code is as follows:
$ (). Ready (function () { $ ("#form-register"). Validate ({ debug: true, onfocusout: function (Element) { $ (element). Valid
(); }, errorelement : ' label ', errorClass: ' text-error ', submithandler: function (form) { form.submit ();
}, errorplacement: function (error, element) { if (element.attr ("name") == ' DAta[captcha] {
error.insertafter ("#captcha_msg"); } else {
error.insertafter (Element); } &NBSP;&NBSP}, rules:{ ' data[username] ': { required: true, minlength: 6, maxlength:20, lettersonly:true, remote:{ url: ' {{ site_url (' Ajax/user_check ')  }} ' type: "POST"  }, ' Data[password] ': { required: &NBSP;TRUE,&NBSP;MINLENGTH:&NBSP;6,&NBSP;MAXLENGTH:50}, ' Data[repassword] ': { required: true, equalto: "#password"}, ' Data[captcha] ': { required: true, minlength: 4,remote:{ url: ' {{ site_url (' Captcha/check ')  }} ' type: "POST" &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP} , messages:{} }); });
Hint Information
Click the Submit button to verify that the message will be automatically added after input.
<label for= "username" class= "text-error" style= ">this field is required".
</label> The default prompt is in English, the following prompts can be saved to messages_zh.js and introduced. /* * translated default messages for the jquery validation plugin
. * Locale: ZH (chinese, Chinese (zhōngwén), Chinese, ?h?) */(function ($) { $.extend ($.validator.messages, { required: "Required fields" remote: "Please fix this field", email: "Please enter the correct format email", url: "Please enter a valid URL", date: "Please enter a valid date", dateISO: "Please enter a valid date (ISO).", number: "Please enter a valid number", &NBSP;&NBSP;&NBSP;&Nbsp; digits: "can only enter integers", creditcard: "Please enter a valid credit card number", equalTo: "Please enter the same value again", accept: "Please enter a string with a valid suffix name", maxlength: $.validator.format ("Please enter a string of up to {0} "), minlength: $.validator.format ("Please enter a string of at least {0} length"),
rangelength: $.validator.format ("Please enter a string between {0} and {1} length") range: $.validator.format ("Please enter a between {0} and { Value between 1} "), max: $.validator.format (" Please enter a maximum of {0} Value "), min: $.validator.format (" Please enter a minimum of {0} Value of ") });
} (JQuery)); The default hint message may not be friendly enough to be configured with a hint, set the messages field to messages:{ ' data[ Username] ': { required: ' Please enter username ', minlength: ' please enter 6-20 English characters ' , maxlength: ' Please enter 6-20 English characters ', lettersonly: ' Please enter 6-20 English characters ', remote: ' This username has been used ' &NBSP;&NBSP;&NBSP;&NBSP;&NBSP}, ' Data[password] ': { required: "Please enter a password" minlength: jquery.format ("Password cannot be less than {0}"Word character "), maxlength: Jquery.format ("Password cannot be greater than {0} characters characters")  }, ' Data[repassword] ': { required: "Please enter confirmation password", equalto: "Two times password input inconsistency"  }, ' data[ CAPTCHA] ': { required:
Please enter the verification code ", minlength: " validation code input Error ", remote: "Validation code input Error" } }
Common settings
Debug: Turns on debugging, which is validated only when set to true, and does not submit the form.
Onfocusout: Loss of focus verification, the above example is the loss of focus on the verification, do not need to click the Submit button.
Errorelement: Used to specify the error hint label, default to label.
Errorclass: Specifies the CSS class name for the error hint, and you can customize the style of the error hint.
Submithandler: You can take over the submit event.
Errorplacement: Specifies that the error is displayed as the location, and the default is after the validation element.
Rules: Validation rule.
Message: Specifies the hint information.
Ignore: No validation for some elements
Custom Validation Methods
Addmethod (Name,method,message) method:
Parameter name is the name of the method you added
Parameter method is a function that receives three parameters (Value,element,param)
Value is the values of the elements,
element is the elements themselves
param is a parameter
such as Additional-methods.js in the lettersonly validation
JQuery.validator.addMethod ("Lettersonly", function (value, Element) {return
this.optional (element) | |/^[a-z]+$/ I.test (value);
}, "letters only");
Here is a small example I wrote:
HTML code
Jquery.validate.js
<script> function checkidcard (num) { var len
= num.length, re; if (len == 15) re = new regexp (/^ (\d{6}) ()? ( \D{2}) (\d{2}) (\d{2}) (\d{3}) $/); else if (len == 18) re = new regexp (/^ (\d{6}) ()? ( \D{4}) (\d{2}) (\d{2}) (\d{3}) (\d) $/); else{ //alert ("Please enter 15 or 18 ID card number, you have entered " +len+ ");
return false; } var a = num.match (RE); if (A != null) {
if (len==15) { var d = new date ("+a[3]+"/"+a[4]+"/"+a[5"); var B = D.getyear () ==a[3]&& (D.getmonth () +1) ==a[4]&&d.getdate () ==a[5]; }else{ var d = new date (a[3]+ "/" +a[4]+ "/" +a[5]); var b = d.getfullyear () ==a[3]&& ( D.getmonth () +1) ==a[4]&&d.getdate () ==a[5]; if (! B) { &Nbsp; //alert ("Enter the ID number " + a[0] + " The date of birth is wrong!) "); return false ; } } return true; } </script> <script type= "Text/javascript" > $. Validator.setdefaults ({ submitHandler: function () { alert ("submitted!"); }  }); // Add Validation method (ID card number verification) jquery.validator.addmethod (" Isidcardno ", function (value, element) { return this.optional (Element) | | checkidcard (value); }, "Please enter your ID number correctly"); $ (). Ready (function () { $ ("#firstform"). Validate (); $ ("#secondform "). Validate ({ /*
errorlabelcontainer: "#messageBox" //the container that displays the error message id wrapper: "Li", The container containing each error message */ rules:{ &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;XM :{ required: true, minlength: 2, maxlength: 5 }, pwd:{ required: true, minlength: 6 }, &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;CONFIRM_ pwd:{ required: true, equalTo: "#pwd" },
f2csrq:{ required: true, date: true }, f2xjzd: { required: true }, f2sfzh:{ /*digits: true, rangelength: [18,20]*/ required: true, isidcardno: true } }, messages:{ xm:{ required: "Please fill in the name", minlength: "character length cannot be less than 2 characters", maxlength: "character length cannot be greater than 5 characters" }, pwd:{ required: "Please fill in the password", minlength: "character length cannot be less than 6 characters" }, confirm_pwd:{ required: "Please enter password again",
equalTo: "Password Inconsistent"
}, f2csrq:{ required: "Please enter date of birth", date: "date format is incorrect (example: 2009/04/07)" },
f2xjzd:{ required: "Please enter Address" }, f2sfzh:{ /*digits: "ID number can only be a number ", rangelength: "Identity number length is 18~20 characters" */ required: "Please enter your ID number", isIdCardNo: "Incorrect ID card number" } }  }); /*// When the input box gets focus, the style setting $ (' input '). Focus (function () { iF ($ (this). Is (": Text") | | $ (This). Is (":p assword") $ (This). addclass (' Focus '); if ($ (this). Hasclass (' Have_tooltip ')) { $ (This). Parent (). Parent (). Removeclass (' Field_normal '). addclass (' Field_focus '); }  }); // Style settings &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NB when the input box loses focussp;$ (' input '). blur (function () { $ (This). Removeclass (' Focus '); if ($ (this). Hasclass (' Have_tooltip ')) { $ (This). Parent (). Parent (). Removeclass (' Field_focus '). addclass (' Field_normal '); }
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP});*/ }); </script>
HTML code
<div id= "header" ></div> <div id= "main" > <form id= "Firstform" method= "Get" action= "" > <fieldset> <legend>jquery Validation </legend> <div id= "XM" class= "Owinput" > <div class= " Owlabel ">
<label class= "req" for= "XM" > surname name :</label> </div> <div class= "Owfield" > <span class= "InP" > <input name= "XM" class= "Required" minlength= "2" > </span> </div> </div> <div id= "XB" class= "Owinput" > <div class= " Owlabel "> <label class= "req" for= "F1pwd" > secret code :</label> </div> <div class= "Owfield" > <span class= "INP" > <input id= "F1pwd" name= "f1pwd" class= "required" minlength= "6" > </ span> </div> </div> <div id= "XB" class= "Owinput" > <div class= "Owlabel" >
<label class= "req" for= "F1PWD2" > Password confirmation :</label> </div> <div class= "Owfield" > <span class= "InP" > <input value= "" name= "F1pwd2" type= "text" class= "required" equalto= "#f1pwd" > </span> </div> </div> <div id= "Csrq" class= "Owinput" > <div class= "Owlabel" > <label class= "req" for= "F1CSRQ" > Birth date : </label> </div> <div class= " Owfield "> <span class= "INP" > <input name= "F1csrq" type= "text" class= " Required date "> </span> </div> </div> <div id= "Xjzd" class= "Owinput" > <div class= "Owlabel" > <label class= "req" for= "F1xjzd" > present place of residence :</label> </ div> <div class= "Owfield" > <span class= "INP" > <input name= "F1xjzd" type= "text" class= "Required" > </span>
</div> </div> <div Id= "Sfzh" class= "Owinput" > <div class= "Owlabel" > <label class= "req" for= "F1sfzh" > ID card number  : </label> </div> <div class= " Owfield "> <span class= "INP" > <input name= "F1sfzh" type= "text" class= " Required isidcardno " minlength=" maxlength= "> </span> </div> </div> </fieldset> <div id= " Regsubmit "> <span id=" Btncreate " class= "regbtn" onmouseover= "btncreatehover (this);" onmouseout= "Btncreateout (this);" > <b class= "TL" ><b class= "TR" ></b></b><button id= "send" type= "submit" > mention Cross </button><b class= "BL" ><b class= "BR" ></b></b> </span> <span id= "Btncancel" class= "cancelbtn" onmouseover= "Btncancelhover" (this); " onmouseout= "Btncancelout (this);" > <b class= "TL" ><b class= "tr" ></b></b><button id= "cancelbtn" type= "button" > Take Eliminate </button><b class= "BL" ><b class= "BR" ></b></b> </span> </div> </form> <form id= "Secondform" > <fieldset> <legend> custom jquery Validation </legend> <div id= "XM" class= "Owinput" > <div class= "Owlabel" > <label class= "req" for= "XM" > Surname name :</label> </div> <div class= "Owfield" > <span class= "INP" > <input name= "XM" type= " Text "> </span> </div> </div> <div id= "XB" class= "Owinput" > <div class= "Owlabel" > <label class= "req" for= "pwd" > secret code :</ label> </div> <div class= " Owfield "> <span class= "INP" > <input id= "pwd" name= "pwd" type= "text" > </span> </ div> </div> <div id= "XB" class= "OWINput "> <div class= "Owlabel" > <label class= "req" for= "confirm_pwd" > Password confirmation :</label> </div>
<div class= "Owfield" > <span
class= "INP" > <input name= "confirm_pwd" type= "text" > </span> </div> </div>
<div id= "Csrq" class= "Owinput" > <div class= "Owlabel" > < label class= "req" for= "F2CSRQ" > Birth date :</label> </div> <div class= "Owfield" > <span class= "INP" > <input name= "F2CSRQ" type= "text" > </span> </div> </div> <div id= "Xjzd" class= "Owinput" > <div class= "Owlabel" > <label class= "req" for= " F2xjzd "> now living Place :</label> </div> <div class= "Owfield" > <span class= "INP" > <input value= " name=" F2xjzd " type=" "Text" > </span> </div> </div> <div id= "Sfzh" class= "Owinput" > <div class= "Owlabel" > <label class= "req" for= "F2sfzh" > ID number :</label> </div> <div class= "Owfield" > <span class= "INP" > <input name= "F2sfzh" type= "text" > </span> </div> </div> </fieldset> <div id= "MessageBox" ></div> <!-- This container is used to summarize display error messages --> <div id= "Regsubmit" > <span id= "Btncreate" class= " Regbtn " onmouseover=" btncreatehover (this); " onmouseout= "Btncreateout (this);" > <b class= "TL" ><b class= "TR" ></b></b><button id= "send" type= "submit" > mention Cross </button><b class= "BL" ><b class= "BR" ></b></b> </span> <span id= "BtnCancel" class= "cancelbtn" onmouseover= "btncancelhover (this);" onmouseout= "Btncancelout (this);" > <b class= "TL" ><b class= "tr" ></b></b><button id= "cancelbtn" type= "button" > Take Eliminate </button><b class= "BL" ><b class= "BR" ></b></b> </span> </div> </form> </div> <div id= "Footer" ></div>