Validating phone number in ruby

前端 未结 6 715
渐次进展
渐次进展 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条回答
  • 2021-01-18 09:48

    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
    "
    
    0 讨论(0)
  • 2021-01-18 09:49

    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

    0 讨论(0)
  • 2021-01-18 09:59

    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.)

    0 讨论(0)
  • 2021-01-18 10:12
    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
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-18 10:13

    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.

    0 讨论(0)
提交回复
热议问题