Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validating phone number in ruby

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
like image 463
Bhushan Lodha Avatar asked Jun 01 '12 10:06

Bhushan Lodha


1 Answers

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

like image 188
Martin Avatar answered Oct 15 '22 05:10

Martin