Validating phone number in ruby

前端 未结 6 757
渐次进展
渐次进展 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条回答
  •  Happy的楠姐
    2021-01-18 10:13

    The rules I used in my perl code for validating NANP phone numbers came from an email sent by Doug Newell to the telnum-l mailing list, which I reproduce below, somewhat simplified to only consider full 10 digit numbers:

    The number 10 digits long.  We'll call this pattern:
        ABC DEF XXXX
    
    A may not be 0 or 1.
    
    B may not be 9.
    
    A+B may not be 37 or 96.
    
    B+C may not be 11.
    
    D may not be 0 or 1.
    

    You may be able to extract a regex from libphonenumber's metadata, but beware, it is GNARLY AS HELL.

提交回复
热议问题