在电话号码中允许使用逗号

时间:2015-12-31 08:42:59

标签: regex knockout.js phone-number knockout-validation

我想允许用户使用Knockout Validation在电话号码字段中输入逗号。

有没有办法用Knockout做到这一点?目前,我使用validPhone验证电话号码,如下面的代码:

this.PhoneNumber = ko.observable("").extend({
  required: { message: "Enter valid Phone Number" },
  validPhone: { message: 'Enter valid Phone Number' },
  maxLength: { params: 12, message: "Phone Number only allows 12 characters" }
});

1 个答案:

答案 0 :(得分:1)

您只需使用pattern validationregular expression

这样的事情:

this.PhoneNumber = ko.observable("").extend({
  required: { message: "Enter valid Phone Number"},
  maxLength: { params: 12, message: "Phone Number only allows 12 characters" }
  pattern: {
    message: 'This isnt a valid phone number',
    params: '^ your phone no. regular expression here  $'
  }