Validating phone number in ruby

前端 未结 6 721
渐次进展
渐次进展 2021-01-18 09:37

What are the rules for validating a North American phone number? Also, is there a regex I can use? Is there a gem to do this?

Here are few rules I have

6条回答
  •  盖世英雄少女心
    2021-01-18 09:48

    Try this

    (?:\+?|\b)[0-9]{10}\b
    

    Explanation

    @"
    (?:         # Match the regular expression below
                   # Match either the regular expression below (attempting the next alternative only if this one fails)
          \+          # Match the character “+” literally
             ?           # Between zero and one times, as many times as possible, giving back as needed (greedy)
       |           # Or match regular expression number 2 below (the entire group fails if this one fails to match)
          \b          # Assert position at a word boundary
    )
    [0-9]       # Match a single character in the range between “0” and “9”
       {10}        # Exactly 10 times
    \b          # Assert position at a word boundary
    "
    

提交回复
热议问题