This is the website of this plugin and I found a Chinese blog. (Although the plugin's name is Jquery.validte.js, the plugin's name is called jquery validation).
Basic usage can go to see Leng article or the above blog, I do not introduce in detail here.
First and foremost, all the fields to be validated are in the form, and the fields must have the name attribute.
For more information, please see the original link: http://www.gbtags.com/gb/share/5765.htm
1. Initialization of the default validate parameter:
This plug-in if you want to use, certainly a lot of pages will be used, after all, if there is a form, you need to verify. And the default language of this plugin is English, so we want to convert it into Chinese. We certainly do not want to write the message again on each page of JS. So we can initialize the default message in the common JS.
- JQuery. Extend(jQuery. Validator. Messages, {
- Required: "required field",
- Remote: "Please fix this field",
- Email: "Please enter the correct format of e-mail",
- URL: "Please enter a valid URL",
- Date: "Please enter a valid date",
- Dateiso: "Please enter a valid date (ISO)." ,
- Number: "Please enter a valid digit",
- 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: jQuery. Validator. Format("Enter a string with a maximum length of {0}"),
- MinLength: jQuery. Validator. Format("Please enter a string with a minimum length of {0}"),
- Rangelength: jQuery. Validator. Format("Please enter a string between {0} and {1}"),
- Range: jQuery. Validator. Format("Please enter a value between {0} and {1}"),
- Max: jQuery. Validator. Format("Enter a value of {0} max"),
- Min: jQuery. Validator. Format("Please enter a value of {0} minimum")
- });
At the same time, we can initialize some of the other parameters:
- $. Validator. SetDefaults({
- Errorclass: "Fielderror", //What class to add when wrong
- Ignore: ". Ignore", //default to ignore validation of those domains
- Ignoretitle: true,
- onkeyup: false,
- Errorplacement: function(error, element) {
- //What to do if there is a mistake
- },
- Submithandler: function(form) {
- //All verification through how to do
- }
- });
2. Add a validation rule
The validation rules are added in the form of a number of cold sub-articles before the article is introduced:
- $("#form"). Validate({
- Debug:True, //debug mode
- Rules: {
- Username: {
- Required:true, //enable required fields
- Rangelength: [3,] //Please enter a value between 3 and 10 bits
- };
- };
- });
The above verification rules are written dead, such as required and rangelength are plug-in good rules, of course, these rules do not necessarily meet our requirements, so what should we do? Jquery.validation provides a way to add a custom rule: Addmethod:
- $. Validator. Addmethod("Usernamecheck", function(value, element) {
- return /^[0-9a-z_a-z]+$/. Test(value);
- }, "Malformed user name");
- $("form"). Validate({
- Rules: {
- Username: {
- Required: true,
- MinLength: 2,
- MaxLength:
- Usernamecheck: true
- Remote: {
- URL: "check_username.jhtml",
- Cache: false
- }
- }
- }
- });
The above plugin verifies that the user name is required, length, whether the user name is legal (can also be verified with the built-in pattern), and uses Ajax to verify that the user name is duplicated or not.
sometimes , you may not have the same name, for example, you want to submit a set of data, your data format may look like this:
- <input Type="Text" class="username" name="Member[0". Username " />
- <input Type="Text" class="username" name="Member[1". Username " />
- <input Type="Text" class="username" name="member[2 ].username " />
- <input Type="Text" class="username" name="Member[3". Username " />
- <input Type="Text" class="username" name="member[4 ].username " />
We certainly do not want to be in the rules of the 5 separate write verification rules, too wasted time, experience, then we can use the Addclassrules method, the same effect, the corresponding class.
- $. Validator. Addclassrules({
- Username: {
- Required: true,
- MinLength: 2,
- MaxLength:
- Usernamecheck: true
- Remote: {
- URL: "check_username.jhtml",
- Cache: false
- }
- }
- });
- $("form"). Validate(); //Don't forget to initialize
3. What should I do if I verify the error?
Usually this plugin is left behind the authentication domain, if we are a row of the same name checkbox? Then he would put the error message behind the first checkbox, which would be ugly, and we would certainly like to put it behind the last checkbox, so we'll use the Errorplacement method:
- $("form"). Validate({
- Errorplacement: function(error, element) {
- if(element). Is("input[type= ' checkbox ']")) {
- Error. AppendTo($(element). Parent()); //Put the last one
- } else {
- $(element). After(error); //Put behind the wrong domain
- }
- }
- });
4. The validation passed, and then what?
We can usually verify after passing, and do not want to submit the form directly, maybe we just use the form to verify, and then submit the form via Ajax, then we may use the Submithandler method:
- $("form"). Validate({
- Submithandler: function(form) {
- //Verify success after doing something you want to do
- Form. Submit(); //If you do not want to commit, return false.
- }
- });
5. How to pass JS Direct verification?
Sometimes, even if we do not edit this domain, we also want to verify that the domain is correct, then what to do? Use the valid () method. This method can function to a form, or to a domain, and at the same time, the True/false value is returned.
- var v = $("input"). Valid();
- var b = $("form"). Valid();
6. There are more features, not here, we can look at the above provided by the official website and blog address.
- Code Demo:http://www.gbtags.com/gb/rtreplayerpreview/1140.htm
For more information, please see the original link: http://www.gbtags.com/gb/share/5765.htm
Jquery.validate.js "Simple and practical Form validation Framework" "Advanced Edition"