Is there a gem that normalizes and format US phone numbers in ruby?

前端 未结 6 2037
悲哀的现实
悲哀的现实 2021-01-02 00:10

I was using phony to format phone numbers (meaning, if I put in xxx-xxx-xxxx it would convert to a string, and also tell if there is a (1) before to remove it).

But

相关标签:
6条回答
  • 2021-01-02 00:24
    def format_phone_numbers(n)
        "(#{n[-10..-8]}) #{n[-7..-5]}-#{n[-4..-1]}"
    end
    
    format_phone_numbers('555555555555')
    

    "(555) 555-5555"

    0 讨论(0)
  • 2021-01-02 00:29
    #RAILS_ROOT/lib/String.rb
    class String
      def convert_to_phone
        number = self.gsub(/\D/, '').split(//)
    
        #US 11-digit numbers
        number = number.drop(1) if (number.count == 11 && number[0] == 1)
    
        #US 10-digit numbers
        number.to_s if (number.count == 10)
    
      end
    
    
      def format_phone
        return "#{self[0,3]}-#{self[3,3]}-#{self[6,4]}"
      end
    end
    
    "585-343-2070".convert_to_phone 
    => "5853432070"
    
    "5853432070".convert_to_phone
    => "5853432070"
    
    "1(585)343-2070".convert_to_phone.format_phone
    => "585-343-2070"
    
    ##Everything formatted as requested in Asker's various comments
    
    0 讨论(0)
  • 2021-01-02 00:30

    I've never seen much in the way of a reliable telephone number formatter because it's just so hard to get it right. Just when you think you've seen everything, some other format comes along and wrecks it.

    Ten digit North American numbers are perhaps the easiest to format, you can use a regular expression, but as soon as you encounter extensions you're in trouble. Still, you can kind of hack it yourself if you want:

    def formatted_number(number)
      digits = number.gsub(/\D/, '').split(//)
    
      if (digits.length == 11 and digits[0] == '1')
        # Strip leading 1
        digits.shift
      end
    
      if (digits.length == 10)
        # Rejoin for latest Ruby, remove next line if old Ruby
        digits = digits.join
        '(%s) %s-%s' % [ digits[0,3], digits[3,3], digits[6,4] ]
      end
    end
    

    This will just wrangle eleven and ten digit numbers into the format you want.

    Some examples:

    formatted_number("1 (703) 451-5115")
     # => "(703) 451-5115"
    formatted_number("555-555-1212")
     # => "(555) 555-1212"
    
    0 讨论(0)
  • 2021-01-02 00:31

    I wrote this regex to match NANPA phone numbers with some conventions (e.g. for extensions) for PHP (thank god those days are over) and converted it over to a Rails validator a few months ago for a project. It works great for me, but it is more pragmatic than strictly to spec.

    # app/validators/phone_number_validator.rb
    class PhoneNumberValidator < ActiveModel::EachValidator
      @@regex = %r{\A(?:1(?:[. -])?)?(?:\((?=\d{3}\)))?([2-9]\d{2})(?:(?<=\(\d{3})\))? ?(?:(?<=\d{3})[.-])?([2-9]\d{2})[. -]?(\d{4})(?: (?:ext|x)\.? ?(\d{1,5}))?\Z}
    
      def validate_each (object, attribute, value)
        if m = value.match(@@regex)
          # format the phone number consistently
          object.send("#{attribute}=", "(#{m[1]}) #{m[2]}-#{m[3]}")
        else
          object.errors[attribute] << (options[:message] || "is not an appropriately formatted phone number")
        end
      end
    end
    
    # app/models/foobar.rb
    class Foobar < ActiveRecord::Base
      validates :phone, phone_number: true
    end
    

    The saved/outputted format is like this: (888) 888-8888. Currently the output strips off the extension because I didn't need it. You can add it back in and change the format pretty easily (see the object.send line.

    0 讨论(0)
  • 2021-01-02 00:41

    Earlier this year, I reviewed a bunch of ruby gems that parse and format phone numbers. They fall into a number of groups (see below). TLDR: I used 'phone'. It might work for you because you can specify a default country code that it uses if your phone number doesn't include one.

    1) US-centric:

    big-phoney (0.1.4)
    phone_wrangler (0.1.3)
    simple_phone_number (0.1.9)

    2) depends on rails or active_record:

    phone_number (1.2.0)
    validates_and_formats_phones (0.0.7)

    3) forks of 'phone' that have been merged back into the trunk:

    elskwid-phone (0.9.9.4)
    tfe-phone (0.9.9.1)

    4) relies on you to know the region ahead of time

    phoney (0.1.0)

    5) Kind of almost works for me

    phone (0.9.9.3)

    6) does not contain the substring 'phone' in the gem name (edit: I see you tried this one)

    phony (1.6.1)

    These groupings may be somewhat unfair or out of date so feel free to comment. I must admit I was a little frustrated at the time at how many people had partially re-invented this particular wheel.

    0 讨论(0)
  • 2021-01-02 00:47

    You could use rails number_to_phone method

    see here: http://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html#method-i-number_to_phone

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