【问题标题】:jQuery Validation Method Phone number begin with plus mark [duplicate]jQuery验证方法电话号码以加号开头[重复]
【发布时间】:2015-03-13 07:19:28
【问题描述】:

我已经写了这个方法来验证手机以加号开头,但它不起作用,谁能解释一下这个问题谢谢大家,

jQuery.validator.addMethod("fnType", function (phone_number, element) {
phone_number = phone_number.replace(/\s+/g, ""); 
return this.optional(element) ||  phone_number.match(/^\\+\d{8,}$/);
}, "Please specify a valid number");

$("#EditView").validate({
    rules: {
        phone_work: {
            fnType: true,
            minlength: 10
        }
    }

【问题讨论】:

    标签: jquery jquery-validate


    【解决方案1】:

    可以这样检查国际电话号码:

    /^\+(?:[0-9] ?){6,14}[0-9]$/
    

    解释:

    ^         # Assert position at the beginning of the string.
    \+        # Match a literal "+" character.
    (?:       # Group but don't capture:
    [0-9]     # Match a digit.
    \x20      # Match a space character
    ?         # between zero and one time.
    )         # End the noncapturing group.
    {6,14}    # Repeat the group between 6 and 14 times.
    [0-9]     # Match a digit.
    $         # Assert position at the end of the string.
    

    【讨论】:

      【解决方案2】:

      使用这个

      $.validator.addMethod('fnType', function(value, element) {
      return value.match(/^\+(?:[0-9] ?){6,14}[0-9]$/);
      },'Enter Valid  phone number');
      

      Click Here for more information

      【讨论】:

        猜你喜欢
        • 2023-01-12
        • 2011-05-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-01
        • 2020-04-30
        相关资源
        最近更新 更多