Validate phone number with JavaScript

前端 未结 26 1734

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

    /^[+]*[(]{0,1}[0-9]{1,3}[)]{0,1}[-\s\./0-9]*$/g
    

    (123) 456-7890
    +(123) 456-7890
    +(123)-456-7890
    +(123) - 456-7890
    +(123) - 456-78-90
    123-456-7890
    123.456.7890
    1234567890
    +31636363634
    075-63546725

    This is a very loose option and I prefer to keep it this way, mostly I use it in registration forms where the users need to add their phone number. Usually users have trouble with forms that enforce strict formatting rules, I prefer user to fill in the number and the format it in the display or before saving it to the database. http://regexr.com/3c53v

提交回复
热议问题