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
- A 10 digit number
- no special characters
- A positive number
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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