Jquery.validate.js "Simple and practical Form validation Framework" "Advanced Edition"

Source: Internet
Author: User

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.

  1. JQuery. Extend(jQuery. Validator. Messages, {
  2. Required: "required field",
  3. Remote: "Please fix this field",
  4. Email: "Please enter the correct format of e-mail",
  5. URL: "Please enter a valid URL",
  6. Date: "Please enter a valid date",
  7. Dateiso: "Please enter a valid date (ISO)." ,
  8. Number: "Please enter a valid digit",
  9. Digits: "can only enter integers",
  10. CreditCard: "Please enter a valid credit card number",
  11. Equalto: "Please enter the same value again",
  12. Accept: "Please enter a string with a valid suffix name",
  13. MaxLength: jQuery. Validator. Format("Enter a string with a maximum length of {0}"),
  14. MinLength: jQuery. Validator. Format("Please enter a string with a minimum length of {0}"),
  15. Rangelength: jQuery. Validator. Format("Please enter a string between {0} and {1}"),
  16. Range: jQuery. Validator. Format("Please enter a value between {0} and {1}"),
  17. Max: jQuery. Validator. Format("Enter a value of {0} max"),
  18. Min: jQuery. Validator. Format("Please enter a value of {0} minimum")
  19. });

At the same time, we can initialize some of the other parameters:

  1. $. Validator. SetDefaults({
  2. Errorclass: "Fielderror", //What class to add when wrong
  3. Ignore: ". Ignore", //default to ignore validation of those domains
  4. Ignoretitle: true,
  5. onkeyup: false,
  6. Errorplacement: function(error, element) {
  7. //What to do if there is a mistake
  8. },
  9. Submithandler: function(form) {
  10. //All verification through how to do
  11. }
  12. });
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:

  1. $("#form"). Validate({
  2. Debug:True, //debug mode
  3. Rules: {
  4. Username: {
  5. Required:true, //enable required fields
  6. Rangelength: [3,] //Please enter a value between 3 and 10 bits
  7. };
  8. };
  9. });

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:

  1. $. Validator. Addmethod("Usernamecheck", function(value, element) {
  2. return /^[0-9a-z_a-z]+$/. Test(value);
  3. }, "Malformed user name");
  4. $("form"). Validate({
  5. Rules: {
  6. Username: {
  7. Required: true,
  8. MinLength: 2,
  9. MaxLength:
  10. Usernamecheck: true
  11. Remote: {
  12. URL: "check_username.jhtml",
  13. Cache: false
  14. }
  15. }
  16. }
  17. });

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:

  1. <input Type="Text" class="username" name="Member[0". Username " />
  2. <input Type="Text" class="username" name="Member[1". Username " />
  3. <input Type="Text" class="username" name="member[2 ].username " />
  4. <input Type="Text" class="username" name="Member[3". Username " />
  5. <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.

  1. $. Validator. Addclassrules({
  2. Username: {
  3. Required: true,
  4. MinLength: 2,
  5. MaxLength:
  6. Usernamecheck: true
  7. Remote: {
  8. URL: "check_username.jhtml",
  9. Cache: false
  10. }
  11. }
  12. });
  13. $("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:

  1. $("form"). Validate({
  2. Errorplacement: function(error, element) {
  3. if(element). Is("input[type= ' checkbox ']")) {
  4. Error. AppendTo($(element). Parent()); //Put the last one
  5. } else {
  6. $(element). After(error); //Put behind the wrong domain
  7. }
  8. }
  9. });
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:

    1. $("form"). Validate({
    2. Submithandler: function(form) {
    3. //Verify success after doing something you want to do
    4. Form. Submit(); //If you do not want to commit, return false.
    5. }
    6. });
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.

    1. var v = $("input"). Valid();
    2. 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.

    1. 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"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.