RegEx for validating US phone number format

后端 未结 2 1831
走了就别回头了
走了就别回头了 2021-01-28 07:16

I searched for phone number validation and ended up with below javascript code

jQuery.validator.addMethod(\"phoneUS\", function(phone_number, element) {
      ph         


        
相关标签:
2条回答
  • 2021-01-28 07:36

    I think this question is already asked with answer as well. Check this A comprehensive regex for phone number validation

    0 讨论(0)
  • 2021-01-28 07:44

    You can add an optional match at the end of the regex

    ( x\d{4})? 
    

    Which will match x followed by digits, that is eg x1234 if it is present

    • ( x\d{4})? the quantifier ? matches zero or one occurence of ( x\d{4})

    The regex can be

    ^\(?(\d{3})\)?[-\. ]?(\d{3})[-\. ]?(\d{4})( x\d{4})?$
    

    For example : http://regex101.com/r/sG9qJ6/1

    0 讨论(0)
提交回复
热议问题