Validate phone number with JavaScript

前端 未结 26 1613

I found this code in some website, and it works perfectly. It validates that the phone number is in one of these formats:
(123) 456-7890 or 123-

26条回答
  •  南笙
    南笙 (楼主)
    2020-11-22 05:16

    What I would do is ignore the format and validate the numeric content:

    var originalPhoneNumber = "415-555-1212";
    
    function isValid(p) {
      var phoneRe = /^[2-9]\d{2}[2-9]\d{2}\d{4}$/;
      var digits = p.replace(/\D/g, "");
      return phoneRe.test(digits);
    }
    

提交回复
热议问题