Javascript Regex - What to use to validate a phone number?

后端 未结 6 659
你的背包
你的背包 2020-12-05 21:04

Could anyone tell me what RegEx would work to validate an international phone number including white space between the numbers and also allowing for these chars: - ( )

6条回答
  •  天命终不由人
    2020-12-05 21:37

    This is a long regex, but it supports both formats (for example 2 to be a valid international number, is MUST start with either + or 00):

    /^(?:(?:\(?(?:00|\+)([1-4]\d\d|[1-9]\d?)\)?)?[\-\.\ \\\/]?)?((?:\(?\d{1,}\)?[\-\.\ \\\/]?){0,})(?:[\-\.\ \\\/]?(?:#|ext\.?|extension|x)[\-\.\ \\\/]?(\d+))?$/i
    

    This allows extensions and a multiple choice of formats and separators.

    Matches:

    • (+351) 282 43 50 50
    • 90191919908
    • 555-8909
    • 001 6867684
    • 001 6867684x1
    • 1 (234) 567-8901
    • 1-234-567-8901 x1234
    • 1-234-567-8901 ext1234
    • 1-234 567.89/01 ext.1234
    • 1(234)5678901x1234
    • (123)8575973
    • (0055)(123)8575973

    On $n, it saves:

    1. Country indicator
    2. Phone number
    3. Extention

    This same answer was given here: A comprehensive regex for phone number validation (direct link to my answer)

提交回复
热议问题