jquery Validate Add custom validation rules (verify mailbox ZIP code) _jquery

Source: Internet
Author: User


Jquery:validate Add custom validation

JQuery.validator.addMethod Add a custom validation rule

Addmethod:name, method, message

Simple instance: Add to a single validation

Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title>validate.js Expansion Verification </title>
<script src= "Http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" ></script>
<script type= "Text/javascript" src= "Jquery.validate.js" ></script>
<script type= "Text/javascript" src= "Validate.expand.js" ></script>
<body>
<form action= "" method= "Get" id= "tinyphp" >
<input type= "text" value= "" name= "Iszipcode"/>
<input type= "Submit" value= "submitted"/>
</form>
<script type= "Text/javascript" >
$ ("#tinyphp"). Validate ({
Adding validation rules
Rules: {
Iszipcode: {//Verify Mailbox
Iszipcode:true
}
}
});
</script>
</body>

Validate.expand.js

Copy Code code as follows:

JQuery.validator.addMethod ("Iszipcode", function (value, Element) {
var tel =/^[0-9]{6}$/;
return this.optional (Element) | | (Tel.test (value));
"Please fill in your zip code correctly");

Add multiple authentication Methods

Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title>validate.js Expansion Verification </title>
<script src= "Http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" ></script>
<script type= "Text/javascript" src= "Jquery.validate.js" ></script>
<script type= "Text/javascript" src= "Validate.expand.js" ></script>
<body>
<form action= "" method= "Get" id= "tinyphp" >
Post code: <input type= "text" value= "" Name= "Iszipcode"/><br/><br/>

Name: <input type= "text" value= "" name= "UserName"/>
<input type= "Submit" value= "submitted"/>
</form>
<script type= "Text/javascript" >
$ ("#tinyphp"). Validate ({
Adding validation rules
Rules: {
Iszipcode: {//Verify Mailbox
Iszipcode:true
},
username:{
Required:true,
Username:true,
Rangelength: [5,10]
}
},

Reset the hint information to omit
messages:{
UserName: {
Required: "Please fill in User name",
Rangelength: "User name must be between 5-10 characters"
}

}
});
</script>
</body>

Validate.expand.js

Copy Code code as follows:

JQuery.validator.addMethod ("UserName", function (value, Element) {
return this.optional (Element) | | /^[\u0391-\uffe5\w]+$/.test (value);
"User name must be between 5-10 characters");

JQuery.validator.addMethod ("Iszipcode", function (value, Element) {
var tel =/^[0-9]{6}$/;
return this.optional (Element) | | (Tel.test (value));
"Please fill in your zip code correctly");

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.