6

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 in mind

  1. A 10 digit number
  2. no special characters
  3. A positive number
dda
  • 6,030
  • 2
  • 25
  • 34
Bhushan Lodha
  • 6,824
  • 7
  • 62
  • 100
  • 1
    Do read [US Phone Number Verification](http://stackoverflow.com/questions/175488/us-phone-number-verification). Also [Is there a gem that normalizes and format US phone numbers in ruby?](http://stackoverflow.com/questions/5912690/is-there-a-gem-that-normalizes-and-format-us-phone-numbers-in-ruby) is not a duplicate but may have useful information. – Gilles 'SO- stop being evil' Jun 01 '12 at 13:15

6 Answers6

7

There are many gems that will do this for you.

Take a look at: http://rubygems.org/search?utf8=%E2%9C%93&query=phone+number

This one looks like it will do what you need -- it essentially implements a regex to validate the phone number: http://rubygems.org/gems/validates_phone_number

For US, Canada (Bermuda, Bahamas... etc and all +1 numbers) there are other rules that the regex should follow. The first digit (after the +1) must be 2-9.

For a full list see: http://en.wikipedia.org/wiki/North_American_Numbering_Plan

Martin
  • 15,820
  • 4
  • 47
  • 56
4

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
"
Cylian
  • 10,970
  • 4
  • 42
  • 55
4

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.

DrHyde
  • 1,546
  • 1
  • 14
  • 14
1

I've written a gem here: https://github.com/travisjeffery/validates_phone_number that will do what you want, if you have any questions or issues let me know, please.

travisjeffery
  • 146
  • 2
  • 4
1

If you want something more advanced than a regex, Twilio has an API that can do this:

Twilio Lookup is a REST API that can:

  • Verify that a phone number exists
  • Format any international phone number into its local standard
  • Determine if a phone number is a cell phone, VOIP or landline
  • Discover information about a phone number’s carrier
  • I haven't used this yet! But it should work, though you do need a (free) Twilio account. (Also I'm not affiliated with Twilio, other than having a free account I'm playing with.)

    coco9nyc
    • 71
    • 2
    • 3
    -1
    class Phne
    def ad
    puts "Enter your phone number"
    re=gets.chomp
    if re= ~/\b^([0-9]{10})$\b/
    puts "Valid phone number"
    else
    puts "Invalid phone number"
    end
    end
    end
    
    obj=Phne.new
    obj.ad
    
    J. Chomel
    • 8,193
    • 15
    • 41
    • 69
    Ram
    • 1
    • 1