Validate phone number with JavaScript

前端 未结 26 1721

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

    I have to agree that validating phone numbers is a difficult task. As for this specific problem i would change the regex from

    /^(()?\d{3}())?(-|\s)?\d{3}(-|\s)\d{4}$/
    

    to

    /^(()?\d{3}())?(-|\s)?\d{3}(-|\s)?\d{4}$/
    

    as the only one more element that becomes unnecessary is the last dash/space.

提交回复
热议问题