繁体   English   中英

使用 JQuery-validation 插件进行正则表达式简单的电话验证

[英]Regex simple phone validation with JQuery-validation plugin

我有一个简单的联系表格。 我需要电话号码的特定正则表达式。 用户可以输入 5-15 个字符。 允许使用空格、-、+ 和数字。 我使用了以下模式,但它不符合我的需求。

jQuery.validator.addMethod(
    'phone',
    function (value, element, params) {
      return (
        this.optional(element) ||
        /\(?([0-9]{3})\)?([ .-]?)([0-9]{3})\2([0-9]{4})/.test(value)
      );
    },
    'Bitte nur erlaubte Zeichen eingeben: [0-9], +, -'
  );

添加开始^和结束$

https://regex101.com/r/bIxPK3/4

 const re = /^\+?\(?([0-9]{3})\)?([ .-]?)([0-9]{3})\2([0-9]{4})$/; function testPhone(value) { return re.test(value) } console.log(testPhone("01729863834jkjj")) console.log(testPhone("(555)-555-5553")) console.log(testPhone("555 555 5553")) console.log(testPhone("+555 555 5553")) console.log(testPhone("+001 555 1234"))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM