Validate phone number with JavaScript

前端 未结 26 1614

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

    Try this one - it includes validation for international formats too.

    /^[+]?(1\-|1\s|1|\d{3}\-|\d{3}\s|)?((\(\d{3}\))|\d{3})(\-|\s)?(\d{3})(\-|\s)?(\d{4})$/g

    This regex validates the following format:

    • (541) 754-3010 Domestic
    • +1-541-754-3010 International
    • 1-541-754-3010 Dialed in the US
    • 001-541-754-3010 Dialed from Germany
    • 191 541 754 3010 Dialed from France

提交回复
热议问题