jquery Basic Learning, jquery form validation plugin-validation

Source: Internet
Author: User
Tags jquery library jquery form validation

The basic part of jquery is done before, so look at the plugin.

About the jquery form validation plugin-validation

Validation Features:

    • Built-in validation rules: 19 Types of built-in validation rules for required, digital, e-mail, URL, and credit card numbers
    • Custom validation rules: You can easily customize validation rules
    • Simple and powerful authentication information hint: Default validation information hint, and provide custom override the default prompt information function
    • Real-time validation: Validation can be triggered by keyup or blur events, not just when the form is submitted

How to use:

  1. Referencing the jquery library and the validation plugin
    <type= "Text/javascript"  src= "Jquery-2.2.0.min.js"> </script><type= "Text/javascript" src = "Jquery.validate.min.js" ></ Script >
  2. Determine which form needs to be validated, then call to
    $ ("#myForm"). Validate ();
  3. Encode the validation rules for different fields, set the corresponding properties of the fields
    <id= "username"  name= "username"  class= "required Email "  minilength=" 2 "/><!-- on the above set required is required, Email verifies if it is a mailbox, and if it is a URL, verify that url,minilength=2 is a minimum length of 2-- --

Better ways to use:

The following gameplay is done to achieve the separation of behavior and structure by using the name attribute to correlate fields and validate the rules of validation, in order to make them unrelated to HTML elements.

    • Page
      <id= "myname"  name= "username"/>
    • Code
         $ ("#myform"). Validate ({        rules:{            username:{                required:true,                minlength:2,                email:true            },            url: "url",            Comment: "Required"        }    );

Internationalization

The authentication information for this plugin is in English by default. Then to get into Chinese, it is necessary to quote a new document:

Jquery.validate.messages_cn.js

Customizing validation information

The Valication plugin makes it easy to customize validation rules.

$ ("#myform"). Validate ({        rules:{            username:{                required:true,                minlength:2,                email:true            }        }        , messages:{            username:{                required: ' Please enter your account email ',
Email: ' Please enter the correct email address '}}} );

Customize validation information and beautify

For example, to add a picture to a validation prompt:

$ ("#myform"). Validate ({rules:{username:{required:true, minlength:2, Email:true}}, Errorelement:"Myerrorelement"//used to create error message prompt labels, Success:function(label) {//callback function executed after validation succeeds            //Label pointing to the above error message label MyerrorelementLabel.text ("")//empty the error message prompt. addclass ("Success");//Adding a custom success class        }    });

Then add the corresponding style to the CSS:

Myerrorelement.error {    background:url ("2.ico") no-repeat 0px 0px;     padding-left:48px;} myerrorelement.success {    background:url ("1.png") no-repeat 0px 0px;     padding-left:48px;}

Custom validation Rules

Here is the code that implements the CAPTCHA function with this jquery plugin

  • Page
    < P >    <  for = "Myvalcode"> captcha </label>    <  ID= "Myvalcode"  name= "Valcode">=7+9  </p>
  • Code
    //Customizing a validation method$.validator.addmethod ("Formula",        function(value,element,param) {returnvalue==eval (param); },        ' Please enter the results of the mathematical formula correctly. '    ); //Call this validation rule$ ("#myform"). Validate ({rules:{username:{required:true, minlength:2, Email:true}, Valcode:{formula:7+9}        }    });

jquery Basic Learning, jquery form validation plugin-validation

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.