How to Format This International Phone Number in Rails

How to format this international phone number in Rails?

You could use the number_to_phone(number, options = {}) method from ActionView::Helpers::NumberHelper

However, the docs point out that this method formats a number into a US phone number (e.g., (555) 123-9876).

Instead you could use this patch which adds the ability to provide number groupings:

:groupings     - Specifies alternate groupings 
(must specify 3-element array; defaults to [3, 3, 4])

So in your case you would call:

number_to_phone('0541754301', :groupings => [4, 3, 3], :delimiter => "-") 

to produce:

0541-754-301

How can I format a phone number in Rails?

If you're using Rails, it's easier to use the built-in #number_to_phone helper instead of custom Javascript.

Converting local numbers into international format (for use with Rails and services such as Nexmo/Twilo)

Google’s opensource libphonenumber project is probably the best tool for any sort of work involving phone numbers. Given a country and phone number, libphonenumber can be used to produce the E.164 format which Twilio prefers. It can also produce example phone numbers to improve the accuracy of the data you collect in the first place. A ruby port is available. An example Twilio app using libphonenumber in php can be found in Twilio Skills training.

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

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.

Ruby regex for international phone numbers while excluding a specific country code


This regex should catch phone numbers starting with '+' followed by the country code (1 to 4 digits) and 4 to 9 digits, with no space/dash/dot.

Use

\A(?!\+33)\+\d{1,3}\d{4,9}\z

See regex proof.

EXPLANATION

--------------------------------------------------------------------------------
\A the beginning of the string
--------------------------------------------------------------------------------
(?! look ahead to see if there is not:
--------------------------------------------------------------------------------
\+ '+'
--------------------------------------------------------------------------------
33 '33'
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
\+ '+'
--------------------------------------------------------------------------------
\d{1,3} digits (0-9) (between 1 and 3 times
(matching the most amount possible))
--------------------------------------------------------------------------------
\d{4,9} digits (0-9) (between 4 and 9 times
(matching the most amount possible))
--------------------------------------------------------------------------------
\z the end of the string

Rails: Phone numbers - international prefix

It seems to me that your problem is this line:

country_code.translations[I18n.locale.to_s] || country_code.international_prefix

according to the documentation examples

# Get a specific translation
c.translation('de') #=> 'Vereinigte Staaten von Amerika'
c.translations['fr'] #=> "États-Unis"

ISO3166::Country.translations # {"DE"=>"Germany",...}
ISO3166::Country.translations('DE') # {"DE"=>"Deutschland",...}

you are actually extracting the country's name first and since it's found the code

country_code.international_prefix

is not executed due to the || condition you have. If you would remove the code to the left of the || it should work. In other words try leaving your method like this:

def international_code
self.country_code = ISO3166::Country[country_code]
country_code.international_prefix
end

Change faker gem phone number format

You can set custom format on the fly like this:

Faker::Base.numerify('+90(###) ### ####')

This will solve your problem.



Related Topics



Leave a reply



Submit