Checking the ruby ​​phone number

What are the rules for checking numbers in North America? Also, is there a regex I can use? Is there a stone for this?

Here are a few rules that I mean.

  • 10 digit number
  • no special characters
  • Positive number
+6
source share
6 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 regular expression to check the phone number: http://rubygems.org/gems/validates_phone_number p>

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

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

+7
source

Try

 (?:\+?|\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 " 
+4
source

The rules that I used in my perl code to verify NANP phone numbers came from a letter sent by Doug Newel to the telnum-l mailing list, which I will reproduce below, is somewhat simplified to consider only 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 can extract the regular expression from the libphonenumber metadata , but be careful, this is GNARLY AS HELL.

+3
source

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

Twilio Lookup is a REST API that can:

Make sure there is a phone number. Format any international phone number to its local standard. Determine if the phone number is a cell phone, VOIP or landline. Detecting phone information Media

I have not used this yet! But it should work, although you need a (free) Twilio account. (Also, I am not affiliated with Twilio, except for the free account I play with.)

+1
source

I wrote a gem here: https://github.com/travisjeffery/validates_phone_number , which will do what you want, if you have any questions or concerns, let me know.

0
source
  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
-1
source

Source: https://habr.com/ru/post/917171/


All Articles