Parsing, validating and creating phone numbers
You can install the phone library as a gem
gem sources -a http://gemcutter.org gem install phonie
You can initialize a new phone object with the number, area code, country code and extension number
Phonie::Phone.new('5125486', '91', '385')
or
Phonie::Phone.new(:number => '5125486', :area_code => '91', :country_code => '385', :extension => '143')
You can create a new phone object by parsing from a string. Phonie::Phone does it’s best to detect the country and area codes:
Phonie::Phone.parse '+385915125486' Phonie::Phone.parse '00385915125486'
If the country or area code isn’t given in the string, you must set it, otherwise it doesn’t work:
Phonie::Phone.parse '091/512-5486', :country_code => '385' Phonie::Phone.parse '(091) 512 5486', :country_code => '385'
If you feel that it’s tedious, set the default country code once (in your config/environment.rb):
Phonie::Phone.default_country_code = '385' Phonie::Phone.parse '091/512-5486' Phonie::Phone.parse '(091) 512 5486'
Same goes for the area code:
Phonie::Phone.parse '451-588', :country_code => '385', :area_code => '47'
or
Phonie::Phone.default_country_code = '385' Phonie::Phone.default_area_code = '47' Phonie::Phone.parse '451-588'
Like it’s stated above, Phone does it’s best to automatically detect the country and area code while parsing. Do do this, phone uses data stored in data/countries.yml
.
Each country code can have a regular expression named area_code
that describes how the area code for that particular country looks like.
If an area_code
regular expression isn’t specified, the default, Phonie::Phone::DEFAULT_AREA_CODE
(correct for the US) is used.
Validating is very relaxed, basically it strips out everything that’s not a number or ‘+’ character:
Phonie::Phone.valid? 'blabla 091/512-5486 blabla'
Formating is done via the format
method. The method accepts a Symbol
or a String
.
When given a string, it interpolates the string with the following fields:
-
%c - country_code (385)
-
%a - area_code (91)
-
%A - area_code with leading zero (091)
-
%n - number (5125486)
-
%f - first @@n1_length characters of number (configured through Phonie::Phone.n1_length), default is 3 (512)
-
%l - last characters of number (5486)
-
%x - the extension number
pn = Phonie::Phone.parse('+385915125486') pn.to_s # => "+385915125486" pn.format("%A/%f-%l") # => "091/512-5486" pn.format("+ %c (%a) %n") # => "+ 385 (91) 5125486"
When given a symbol it is used as a lookup for the format in the Phonie::Phone.named_formats
hash.
pn.format(:europe) # => "+385 (0) 91 512 5486" pn.format(:us) # => "(234) 123 4567" pn.format(:default_with_extension) # => "+3851234567x143"
You can add your own custom named formats like so:
Phonie::Phone.named_formats[:short] = '%A/%n1-%n2' pn.format(:short) # => 091/512-5486
Parse testing for different countries.
Currently tested on:
- AE
-
UAE
- AF
-
Afghanistan
- AL
-
Albania
- AM
-
Armenia
- AR
-
Argentina
- AT
-
Austria
- AU
-
Australia
- BA
-
Bosnia and Herzegovina
- BD
-
Bangladesh
- BE
-
Belgium
- BG
-
Bulgaria
- BO
-
Bolivia
- BR
-
Brazil
- BT
-
Bhutan
- BY
-
Belarus
- BZ
-
Belize
- CA
-
Canada
- CR
-
Costa Rica
- CY
-
Cyprus
- CZ
-
Czech Republic
- DE
-
Germany
- DK
-
Denmark
- DZ
-
Algeria
- EC
-
Ecuador
- EE
-
Estonia
- EG
-
Egypt
- ET
-
Ethiopia
- FI
-
Finland
- FR
-
France
- GB
-
United Kingdom
- GE
-
Georgia
- GH
-
Ghana
- GR
-
Greece
- GT
-
Guatemala
- GU
-
Guam
- GY
-
Guyana
- HR
-
Croatia
- HU
-
Hungary
- IL
-
Israel
- IN
-
India
- IR
-
Iran
- IT
-
Italy
- KE
-
Kenya
- LK
-
Sri Lanka
- LT
-
Lithuania
- LU
-
Luxembourg
- LV
-
Latvia
- NG
-
Nigeria
- NL
-
Netherlands
- NO
-
Norway
- NP
-
Nepal
- PH
-
Philippines
- PK
-
Pakistan
- PT
-
Portugal
- QA
-
Qatar
- RS
-
Serbia
- RU
-
Russian Federation
- SA
-
Saudi Arabia
- SE
-
Sweden
- SI
-
Slovenia
- SV
-
El Salvador
- TO
-
Tonga
- UA
-
Ukraine
- US
-
United States
- UY
-
Uruguay
- ZA
-
South Africa
- ZW
-
Zimbabwe
More testing is needed to add support for missing countries, and improve support for tested countries. In many cases only minimal testing is done on area codes, local number formats and number length where more exact matching is possible.
The best places to start is to read through the country tests and data/phone_countries.rb
This is based off a fork of the Phone gem (github.com/carr/phone), and was extensively modified for better support of country detection
Another fork based off this one is available at github.com/capitainetrain/phoner It explictly requires ActiveSupport.
Tomislav Carr, Don Morrison, Michael Squires, Todd Eichel (Fooala, Inc.), chipiga, Etienne Samson, Luke Randall, Wesley Moxam