Two methods for jquery validate plug-in ajax verification repetition

Source: Internet
Author: User
Tags repetition


Let's briefly introduce the js code. For more information, see the source code of the above example:


$ (Document). ready (function (){
 
JQuery. validator. addMethod ("phonecheck", function (value, element) {// add a phonecheck method to customize check rules
String = value. match (/0 (\ d {2})-(\ d {7})/ig );
If (string! = Null ){
Return true;
} Else {
Return false;
      } 
}, "Telphone number like 021-1234567 ");
 
JQuery. validator. addMethod ("phonesame", function (value, element) {// use the jquery ajax method to verify whether the phone already exists
Var flag = 1;
$. Ajax ({
Type: "POST ",
Url: 'Tel. Php ',
Async: false, // synchronous method. If asynchronous, the flag is always 1
Data: {'tel': value },
Success: function (msg ){
If (msg = 'yes '){
Flag = 0;
               } 
          } 
});
 
If (flag = 0 ){
Return false;
} Else {
Return true;
      } 
 
}, "Sorry number have been exist ");
 
$ ("# Myform"). validate ({
ErrorPlacement: function (error, element ){
Error. insertAfter (element );
},
Rules :{
Username :{
Required: true,
Remote: {// methods for remote verification
Url: "tel. php ",
Type: "post ",
DataType: "html ",
Data :{
Username: function () {return $ ("# username"). val ();}
},
DataFilter: function (data, type ){
If (data = "yes ")
Return true;
Else
Return false;
                 } 
              } 
},
Telphone :{
Required: true,
Rangelength: [11, 11],
Phonecheck: true,
Phonesame: true
          } 
},
Messages :{
Telphone :{
Required: "Please enter your phone ",
Rangelength: "phone must be 11 numbers"
},
Username :{
Required: "Please enter your username ",
Remote: "the username have been exist"
          } 
},
Debug: true
})
});
</Script>
We recommend that you use jquery validate, which is easy to use.

Related Article

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.