【问题标题】:make sure first three digits of phone number are not 0's, using javascript使用javascript确保电话号码的前三位不是0
【发布时间】:2020-07-09 19:28:48
【问题描述】:

我正在尝试使用 javascript 验证电话号码,但我被困在这部分

区号(999 中的前 3 个数字)不能全为零 (0)

我知道生成我想要的任何格式的代码(比如 xxx-xxx-xxxx),但我如何确保前 0 不是全为零?

感谢任何帮助,谢谢!

【问题讨论】:

  • var isValid = !/^000/.test(str);

标签: javascript html forms validation


【解决方案1】:

您可以通过多种方式做到这一点,这里有几个使用不同方法的示例。

使用startsWith

 var num = "000-xxx-xxxx";

 if (num.startsWith("000") === true) {
      console.log("Number starts with 000");
 }

使用 substr

 var num = "000-xxx-xxxx";
 var first_three = num.substr(0, 3);

 if (first_three === "000") {
      console.log("Number starts with 000");
 }

使用拆分

 var num = "000-xxx-xxxx";
 var first_three = num.split("-")[0];

 if (first_three === "000") {
      console.log("Number starts with 000");
 }

使用正则表达式

 var num = "000-xxx-xxxx";

 if (/^000/.test(num)) {
      console.log("Number starts with 000");
 }

【讨论】:

    【解决方案2】:

    你可以使用parseInt,它会忽略字符串中第一个非数字字符之后的所有内容:

    var phone1 = '000-555-4444';
    var phone2 = '555-555-5555';
    
    function isValidAreaCode(phoneNumber) {
       return parseInt(phoneNumber, 10) > 0;
    }
    
    console.log(phone1, 'valid?', isValidAreaCode(phone1));
    console.log(phone2, 'valid?', isValidAreaCode(phone2));

    【讨论】:

      【解决方案3】:

      您可以使用^[0]{3}$^\d{3}$

      【讨论】:

        【解决方案4】:

        假设您正在测试美国区号,使用正则表达式 /^[2-9][0-8][0-9]/ 测试它们应该可以工作。 According to this。 区号可以以 2 到 9 之间的数字开头,第二个数字可以是任何 除 9 以外的数字,最后一个数字可以是任意数字。

        function hasValidAreaCode(num) {
          var stripped = num.replace(/\D/g, ''); // remove any no-numeric characters
          return /^[2-9][0-8][0-9]/.test(stripped);
        }
        

        交互示例:

        function hasValidAreaCode(num) {
          var stripped = num.replace(/\D/g, ''); // remove any no-numeric characters
          return /^[2-9][0-8][0-9]/.test(stripped);
        }
        
        var elPhonenumber = document.getElementById('phonenumber');
        elPhonenumber.addEventListener('keyup', function (event) {
        
          var v = elPhonenumber.value;
          if (v.replace(/\D/g, '').length > 2) {
            var valid = hasValidAreaCode(v);
            if (valid) {
              elPhonenumber.classList.add('valid');
              elPhonenumber.classList.remove('invalid');
            } else {
              elPhonenumber.classList.remove('valid');
              elPhonenumber.classList.add('invalid');
            }
          } else {
            elPhonenumber.classList.remove('valid', 'invalid');
          }
         });
        .valid, .invalid {
          color: #000;
        }
        .valid {
          color: green;
        }
        .invalid {
          color: red;
        }
        <label for="phonenumber">Please enter a phonenumber</label> <input id="phonenumber">

        【讨论】:

          猜你喜欢
          • 2011-07-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-11-30
          相关资源
          最近更新 更多