2015-06-27 91 views
1

如何使用addMethod()方法编写新方法?我想验证一个电话号码,它以0开始,它的长度是10。我试过到目前为止:添加方法jQuery验证

function cell(element, value){ 
      if (!/^[0]\d{9}$/.test(element).val()) { 
       return false; 
      } 
      else { 
       return true; 
      } 
     } 

     $.validator.addMethod("cell", cell, "This phone number is incorrect"); 

和:

$("#signupForm").validate({ 
    rules: { 
     cell: { 
      required: true, 
      digits: true, 
      cell: true 
     } 
    } 
}) 

而在消息我应该在写'cell'的消息给?谢谢!

+0

[自定义方法与jQuery验证插件]的可能重复(http://stackoverflow.com/questions/15589152/custom-method-with-jquery-validation-plugin) –

回答

0

添加自定义的方法,这way-

$.validator.addMethod("cell", function (value, element) { 
     if (!/^[0]\d{9}$/.test(element).val()) { 
      return false; 
     } 
     else { 
      return true; 
     } 
}, 'This phone number is incorrect'); 

和您的验证方法 -

$("#signupForm").validate({ 
    rules: { 
     cell: { 
      required: true, 
      digits: true, 
      cell: true 
     } 
    } 
}); 

它应该工作。

+0

它的工作!谢谢:) – eitanmayer

+0

不客气。 –