Validate phone number with JavaScript

前端 未结 26 1689

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:26

    Where str could be any of these formarts: 555-555-5555 (555)555-5555 (555) 555-5555 555 555 5555 5555555555 1 555 555 5555

    function telephoneCheck(str) {
      var isphone = /^(1\s|1|)?((\(\d{3}\))|\d{3})(\-|\s)?(\d{3})(\-|\s)?(\d{4})$/.test(str);
      alert(isphone);
    }
    telephoneCheck("1 555 555 5555");

提交回复
热议问题