RegEx for Phone number in JavaScript

后端 未结 3 744
忘掉有多难
忘掉有多难 2020-12-20 02:17

I was using RegEx to validate user phone numbers. I have set of requirements for phone number validation. I dont have much idea about the RegEX. can anyone help me to provid

相关标签:
3条回答
  • 2020-12-20 02:26

    based on you samples try this pattern.

    ^(?:\+?\d{2}[ -]?[\d -][\d -]+)$
    
    0 讨论(0)
  • 2020-12-20 02:27

    Just to help you build some concepts.
    The following regex would match the first seven inputs you provided.

    /^\+?\d{2}[- ]?\d{3}[- ]?\d{5}$/
    

    \+? would match a + sign. The ? in it makes the + sign optional.
    \d{2} matches two digits
    [- ]? matches either a - or a (space). ? makes the occurrence of - or (space) optional.
    \d{5} then matches 5 digits.
    ^ and $ are the start and end anchors.

    0 讨论(0)
  • 2020-12-20 02:39

    Based on your samples, try this:

    ^(?:\+?\d{2}[ -]?\d{3}[ -]?\d{5}|\d{4})$
    

    It will match all the correct values.

    DESCRIPTION:

    Regular expression visualization

    DEMO:

    http://regexr.com?340nf

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