Phone validation regex

后端 未结 16 1514
孤城傲影
孤城傲影 2020-11-27 02:56

I\'m using this pattern to check the validation of a phone number

^[0-9\\-\\+]{9,15}$

It\'s works for 0771234567 and +0

相关标签:
16条回答
  • 2020-11-27 03:40

    Adding to @Joe Johnston's answer, this will also accept:

    +16444444444,,241119933
    

    (Required for Apple's special character support for dial-ins - https://support.apple.com/kb/PH18551?locale=en_US)

    \(?\+[0-9]{1,3}\)? ?-?[0-9]{1,3} ?-?[0-9]{3,5} ?-?[0-9]{4}( ?-?[0-9]{3})? ?([\w\,\@\^]{1,10}\s?\d{1,10})?
    

    Note: Accepts upto 10 digits for extension code

    0 讨论(0)
  • 2020-11-27 03:43

    Here is the regex for Ethiopian Phone Number. For my fellow Ethiopian developers ;)

    phoneExp = /^(^\+251|^251|^0)?9\d{8}$/;
    

    It matches the following (restrict any unwanted character in start and end position)

    • +251912345678
    • 251912345678
    • 0912345678
    • 912345678

    You can test it on this site regexr.

    0 讨论(0)
  • 2020-11-27 03:45

    This regex matches any number with the common format 1-(999)-999-9999 and anything in between. Also, the regex will allow braces or no braces and separations with period, space or dash. "^([01][- .])?(\(\d{3}\)|\d{3})[- .]?\d{3}[- .]\d{4}$"

    0 讨论(0)
  • 2020-11-27 03:47

    Consider:

    ^\+?[0-9]{3}-?[0-9]{6,12}$
    

    This only allows + at the beginning; it requires 3 digits, followed by an optional dash, followed by 6-12 more digits.

    Note that the original regex allows 'phone numbers' such as 70+12---12+92, which is a bit more liberal than you probably had in mind.


    The question was amended to add:

    +077-1-23-45-67 and +077-123-45-6-7

    You now probably need to be using a regex system that supports alternatives:

    ^\+?[0-9]{3}-?([0-9]{7}|[0-9]-[0-9]{2}-[0-9]{2}-[0-9]{2}|[0-9]{3}-[0-9]{2}-[0-9]-[0-9])$
    

    The first alternative is seven digits; the second is 1-23-45-67; the third is 123-45-6-7. These all share the optional plus + followed by 3 digits and an optional dash - prefix.

    The comment below mentions another pattern:

    +077-12-34-567

    It is not at all clear what the general pattern should be - maybe one or more digits separated by dashes; digits at front and back?

    ^\+?[0-9]{3}-?[0-9](-[0-9]+)+$
    

    This will allow the '+077-' prefix, followed by any sequence of digits alternating with dashes, with at least one digit between each dash and no dash at the end.

    0 讨论(0)
  • 2020-11-27 03:48
    ^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$
    

    Matches the following cases:

    123-456-7890

    (123) 456-7890

    123 456 7890

    123.456.7890

    +91 (123) 456-7890

    0 讨论(0)
  • 2020-11-27 03:53

    Try this

    \+?\(?([0-9]{3})\)?[-.]?\(?([0-9]{3})\)?[-.]?\(?([0-9]{4})\)?
    

    It matches the following cases

    • +123-(456)-(7890)
    • +123.(456).(7890)
    • +(123).(456).(7890)
    • +(123)-(456)-(7890)
    • +123(456)(7890)
    • +(123)(456)(7890)
    • 123-(456)-(7890)
    • 123.(456).(7890)
    • (123).(456).(7890)
    • (123)-(456)-(7890)
    • 123(456)(7890)
    • (123)(456)(7890)

    For further explanation on the pattern CLICKME

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