albeebe / phoneformat.js Goto Github PK
View Code? Open in Web Editor NEWJavascript Phone Number Formatter
License: MIT License
Javascript Phone Number Formatter
License: MIT License
Hi,
Thanks for the great work on this, really appreciate it a lot.
Was wondering if you'd consider adding bower support for this project (http://bower.io/)
Doing a bower install phoneformat would be helluva easier =)
The minimised version of phone-format-global.min.js
throw the following error when embedded as a script in html file:
Uncaught SyntaxError: Invalid regular expression: /[֑-ۯۺ-߿��-﷿ﹰ-ﻼ]/: Range out of order in character class
I am wondering if some tools wrongly minimize the lib from google.
Hi, i tried to update my version of phoneformat.js following your instructions because I tried a valid number it didn't recognize as so. But the compillation failed and i got these errors:
Number of errors: 9
JSC_MISSING_PROVIDE_ERROR: required "goog.dom" namespace never provided at line 13 character 0
goog.require('goog.dom');
^
JSC_MISSING_PROVIDE_ERROR: required "goog.json" namespace never provided at line 14 character 0
goog.require('goog.json');
^
JSC_MISSING_PROVIDE_ERROR: required "goog.proto2.ObjectSerializer" namespace never provided at line 15 character 0
goog.require('goog.proto2.ObjectSerializer');
^
JSC_MISSING_PROVIDE_ERROR: required "goog.string.StringBuffer" namespace never provided at line 16 character 0
goog.require('goog.string.StringBuffer');
^
JSC_MISSING_PROVIDE_ERROR: required "i18n.phonenumbers.AsYouTypeFormatter" namespace never provided at line 17 character 0
goog.require('i18n.phonenumbers.AsYouTypeFormatter');
^
JSC_MISSING_PROVIDE_ERROR: required "i18n.phonenumbers.PhoneNumberFormat" namespace never provided at line 18 character 0
goog.require('i18n.phonenumbers.PhoneNumberFormat');
^
JSC_MISSING_PROVIDE_ERROR: required "i18n.phonenumbers.PhoneNumberType" namespace never provided at line 19 character 0
goog.require('i18n.phonenumbers.PhoneNumberType');
^
JSC_MISSING_PROVIDE_ERROR: required "i18n.phonenumbers.PhoneNumberUtil" namespace never provided at line 20 character 0
goog.require('i18n.phonenumbers.PhoneNumberUtil');
^
JSC_MISSING_PROVIDE_ERROR: required "i18n.phonenumbers.PhoneNumberUtil.ValidationResult" namespace never provided at line 21 character 0
goog.require('i18n.phonenumbers.PhoneNumberUtil.ValidationResult');
^
Hi!!
I've found a problem with phoneformat library.
I've used http://i18next.com/ library to do translations The problem is that it also declares a i18n global variable as phoneformat.js then this global variable is overwritten and the library doesn't work correctly.
I think that you could change this global variable name for other because i18n is a common library to manage translations and a lot of people use it.
Thank you.
Hai Al Cheetah -
This is wonderful plugin, am not familiar in javascript, but i need to validate phonenumber based on country like google signup validator, how can i inculude this plugin in to my Jquery validation plugin. if you can please help me. Thank you.
It seems as you forgot to pull the changes before creating the 1.0.1 tag. So the tagged version in actually the same version as 1.0.0. This will cause bower to download the wrong version.
You need to remove the local and remote 1.0.1 tag, pull the latest changes t local local repo and then recreate and push the 1.0.1 tag again. Here's a cheatsheet
git tag -d 1.0.1
git push --delete origin 1.0.1
git pull
git tag 1.0.1
git push origin 1.0.1
I'm using webpack to build my project and I included this as my library. However this library add over 500+ KB to my project and that's a considerable size:
[526] ./~/phoneformat.js/dist/phone-format-exports.js 507 kB {0} [built]
Is there a way to shrink the size of this library?
I can't install phoneformat.js as a dependency in my npm's package.json, because the name field is missing.
Thanks !
Libphonenumber offers handling of extensions using the x1234 or ext.1234 format. Can you add that, please?
Also, how would I write a function to return my Local format number broken into the parts? I'm only doing US numbers so it would be CountryCode, AreaCode and Number, plus the extension, if you add it.
Steps to reproduce:
Expected: Function returns true
Actual: Function returns false
The problem appears to be the missing country code in the call to parseAndKeepRawInput in isValidNumber:
var number = phoneUtil.parseAndKeepRawInput(phone, "");
The call is throwing an invalid country code exception.
Try entering the same number at PhoneFormat.com and note the number is reported as valid. This is because the site is using an older version PhoneFormat.js, before the country code was removed in this commit: 46a2fa4
phoneFormat().formatLocal(...);
isValidNumber does not take in a country input, but uses the global country variable set in source.js on the phoneformat.com site. This method does not take in a country and uses an empty string as the country code in the library version of PhoneFormat.js.
You actually need to add this to Bower.IO
bower register phoneformatjs https://github.com/albeebe/phoneformat.js
elad/phoneformat#4 is currently using phoneformat
It would be really good if there was a bower.json with main defined because now it's required to manually override this in the project bower.json and define the main there.
Nice library! Thanks for this!
We are trying to incorporate your library in our solution. And we've two questions to ask you:
isValidNumber("9998063528", "India");
Now, to do that, we've created one function which returns country code from Country name e.g.
var aCountryCode = countryNameToCode("India");
/Convert the country name to a code
country name (String) to 2 digit country code/
function countryNameToCode(countryName) {
var arrCountryCode = new Array();
arrCountryCode['Afghanistan'] = "AF";
arrCountryCode['Albania'] = "AL";
arrCountryCode['Algeria'] = "DZ";
arrCountryCode['American Samoa'] = "AS";
arrCountryCode['Andorra'] = "AD";
arrCountryCode['Angola'] = "AO";
arrCountryCode['Anguilla'] = "AI";
arrCountryCode['Antarctica'] = "AQ";
arrCountryCode['Antigua And Barbuda'] = "AG";
arrCountryCode['Argentina'] = "AR";
arrCountryCode['Armenia'] = "AM";
arrCountryCode['Aruba'] = "AW";
arrCountryCode['Ascension Island'] = "AC";
arrCountryCode['Australia'] = "AU";
arrCountryCode['Austria'] = "AT";
arrCountryCode['Azerbaijan'] = "AZ";
arrCountryCode['Bahamas'] = "BS";
arrCountryCode['Bahrain'] = "BH";
arrCountryCode['Bangladesh'] = "BD";
arrCountryCode['Barbados'] = "BB";
arrCountryCode['Belarus'] = "BY";
arrCountryCode['Belgium'] = "BE";
arrCountryCode['Belize'] = "BZ";
arrCountryCode['Benin'] = "BJ";
arrCountryCode['Bermuda'] = "BM";
arrCountryCode['Bhutan'] = "BT";
arrCountryCode['Bolivia'] = "BO";
arrCountryCode['Bosnia And Herzegovina'] = "BA";
arrCountryCode['Botswana'] = "BW";
arrCountryCode['Bouvet Island'] = "BV";
arrCountryCode['Brazil'] = "BR";
arrCountryCode['British Indian Ocean Territory'] = "IO";
arrCountryCode['Brunei'] = "BN";
arrCountryCode['Bulgaria'] = "BG";
arrCountryCode['Burkina Faso'] = "BF";
arrCountryCode['Burundi'] = "BI";
arrCountryCode['Cambodia'] = "KH";
arrCountryCode['Cameroon'] = "CM";
arrCountryCode['Canada'] = "CA";
arrCountryCode['Cape Verde'] = "CV";
arrCountryCode['Cayman Islands'] = "KY";
arrCountryCode['Central African Republic'] = "CF";
arrCountryCode['Chad'] = "TD";
arrCountryCode['Chile'] = "CL";
arrCountryCode['China'] = "CN";
arrCountryCode['Christmas Island'] = "CX";
arrCountryCode['Cocos (Keeling) Islands'] = "CC";
arrCountryCode['Columbia'] = "CO";
arrCountryCode['Comoros'] = "KM";
arrCountryCode['Congo'] = "CG";
arrCountryCode['Cook Islands'] = "CK";
arrCountryCode['Costa Rica'] = "CR";
arrCountryCode['Cote D\'Ivorie (Ivory Coast)'] = "CI";
arrCountryCode['Croatia (Hrvatska)'] = "HR";
arrCountryCode['Cuba'] = "CU";
arrCountryCode['Cyprus'] = "CY";
arrCountryCode['Czech Republic'] = "CZ";
arrCountryCode['Democratic Republic Of Congo (Zaire)'] = "CD";
arrCountryCode['Denmark'] = "DK";
arrCountryCode['Djibouti'] = "DJ";
arrCountryCode['Dominica'] = "DM";
arrCountryCode['Dominican Republic'] = "DO";
arrCountryCode['East Timor'] = "TL";
arrCountryCode['Ecuador'] = "EC";
arrCountryCode['Egypt'] = "EG";
arrCountryCode['El Salvador'] = "SV";
arrCountryCode['Equatorial Guinea'] = "GQ";
arrCountryCode['Eritrea'] = "ER";
arrCountryCode['Estonia'] = "EE";
arrCountryCode['Ethiopia'] = "ET";
arrCountryCode['Falkland Islands (Malvinas)'] = "FK";
arrCountryCode['Faroe Islands'] = "FO";
arrCountryCode['Fiji'] = "FJ";
arrCountryCode['Finland'] = "FI";
arrCountryCode['France'] = "FR";
arrCountryCode['France, Metropolitan'] = "FX";
arrCountryCode['French Guinea'] = "GF";
arrCountryCode['French Polynesia'] = "PF";
arrCountryCode['French Southern Territories'] = "TF";
arrCountryCode['Gabon'] = "GA";
arrCountryCode['Gambia'] = "GM";
arrCountryCode['Georgia'] = "GE";
arrCountryCode['Germany'] = "DE";
arrCountryCode['Ghana'] = "GH";
arrCountryCode['Gibraltar'] = "GI";
arrCountryCode['Greece'] = "GR";
arrCountryCode['Greenland'] = "GL";
arrCountryCode['Grenada'] = "GD";
arrCountryCode['Guadeloupe'] = "GP";
arrCountryCode['Guam'] = "GU";
arrCountryCode['Guatemala'] = "GT";
arrCountryCode['Guinea'] = "GN";
arrCountryCode['Guinea-Bissau'] = "GW";
arrCountryCode['Guyana'] = "GY";
arrCountryCode['Haiti'] = "HT";
arrCountryCode['Heard And McDonald Islands'] = "HM";
arrCountryCode['Honduras'] = "HN";
arrCountryCode['Hong Kong'] = "HK";
arrCountryCode['Hungary'] = "HU";
arrCountryCode['Iceland'] = "IS";
arrCountryCode['India'] = "IN";
arrCountryCode['Indonesia'] = "ID";
arrCountryCode['Iran'] = "IR";
arrCountryCode['Iraq'] = "IQ";
arrCountryCode['Ireland'] = "IE";
arrCountryCode['Isle of Man'] = "IM";
arrCountryCode['Israel'] = "IL";
arrCountryCode['Italy'] = "IT";
arrCountryCode['Jamaica'] = "JM";
arrCountryCode['Japan'] = "JP";
arrCountryCode['Jordan'] = "JO";
arrCountryCode['Kazakhstan'] = "KZ";
arrCountryCode['Kenya'] = "KE";
arrCountryCode['Kiribati'] = "KI";
arrCountryCode['Kuwait'] = "KW";
arrCountryCode['Kyrgyzstan'] = "KG";
arrCountryCode['Laos'] = "LA";
arrCountryCode['Latvia'] = "LV";
arrCountryCode['Lebanon'] = "LB";
arrCountryCode['Lesotho'] = "LS";
arrCountryCode['Liberia'] = "LR";
arrCountryCode['Libya'] = "LY";
arrCountryCode['Liechtenstein'] = "LI";
arrCountryCode['Lithuania'] = "LT";
arrCountryCode['Luxembourg'] = "LU";
arrCountryCode['Macau'] = "MO";
arrCountryCode['Macedonia'] = "MK";
arrCountryCode['Madagascar'] = "MG";
arrCountryCode['Malawi'] = "MW";
arrCountryCode['Malaysia'] = "MY";
arrCountryCode['Maldives'] = "MV";
arrCountryCode['Mali'] = "ML";
arrCountryCode['Malta'] = "MT";
arrCountryCode['Marshall Islands'] = "MH";
arrCountryCode['Martinique'] = "MQ";
arrCountryCode['Mauritania'] = "MR";
arrCountryCode['Mauritius'] = "MU";
arrCountryCode['Mayotte'] = "YT";
arrCountryCode['Mexico'] = "MX";
arrCountryCode['Micronesia'] = "FM";
arrCountryCode['Moldova'] = "MD";
arrCountryCode['Monaco'] = "MC";
arrCountryCode['Mongolia'] = "MN";
arrCountryCode['Montenegro'] = "ME";
arrCountryCode['Montserrat'] = "MS";
arrCountryCode['Morocco'] = "MA";
arrCountryCode['Mozambique'] = "MZ";
arrCountryCode['Myanmar (Burma)'] = "MM";
arrCountryCode['Namibia'] = "NA";
arrCountryCode['Nauru'] = "NR";
arrCountryCode['Nepal'] = "NP";
arrCountryCode['Netherlands'] = "NL";
arrCountryCode['Netherlands Antilles'] = "AN";
arrCountryCode['New Caledonia'] = "NC";
arrCountryCode['New Zealand'] = "NZ";
arrCountryCode['Nicaragua'] = "NI";
arrCountryCode['Niger'] = "NE";
arrCountryCode['Nigeria'] = "NG";
arrCountryCode['Niue'] = "NU";
arrCountryCode['Norfolk Island'] = "NF";
arrCountryCode['North Korea'] = "KP";
arrCountryCode['Northern Mariana Islands'] = "MP";
arrCountryCode['Norway'] = "NO";
arrCountryCode['Oman'] = "OM";
arrCountryCode['Pakistan'] = "PK";
arrCountryCode['Palau'] = "PW";
arrCountryCode['Palestine'] = "PS";
arrCountryCode['Panama'] = "PA";
arrCountryCode['Papua New Guinea'] = "PG";
arrCountryCode['Paraguay'] = "PY";
arrCountryCode['Peru'] = "PE";
arrCountryCode['Philippines'] = "PH";
arrCountryCode['Pitcairn'] = "PN";
arrCountryCode['Poland'] = "PL";
arrCountryCode['Portugal'] = "PT";
arrCountryCode['Puerto Rico'] = "PR";
arrCountryCode['Qatar'] = "QA";
arrCountryCode['Reunion'] = "RE";
arrCountryCode['Romania'] = "RO";
arrCountryCode['Russia'] = "RU";
arrCountryCode['Rwanda'] = "RW";
arrCountryCode['Saint Helena'] = "SH";
arrCountryCode['Saint Kitts And Nevis'] = "KN";
arrCountryCode['Saint Lucia'] = "LC";
arrCountryCode['Saint Pierre And Miquelon'] = "PM";
arrCountryCode['Saint Vincent And The Grenadines'] = "VC";
arrCountryCode['San Marino'] = "SM";
arrCountryCode['Sao Tome And Principe'] = "ST";
arrCountryCode['Saudi Arabia'] = "SA";
arrCountryCode['Senegal'] = "SN";
arrCountryCode['Serbia'] = "RS";
arrCountryCode['Seychelles'] = "SC";
arrCountryCode['Sierra Leone'] = "SL";
arrCountryCode['Singapore'] = "SG";
arrCountryCode['Slovak Republic'] = "SK";
arrCountryCode['Slovenia'] = "SI";
arrCountryCode['Solomon Islands'] = "SB";
arrCountryCode['Somalia'] = "SO";
arrCountryCode['South Africa'] = "ZA";
arrCountryCode['South Georgia And South Sandwich Islan'] = "GS";
arrCountryCode['South Korea'] = "KR";
arrCountryCode['Spain'] = "ES";
arrCountryCode['Sri Lanka'] = "LK";
arrCountryCode['Sudan'] = "SD";
arrCountryCode['Suriname'] = "SR";
arrCountryCode['Svalbard And Jan Mayen'] = "SJ";
arrCountryCode['Swaziland'] = "SZ";
arrCountryCode['Sweden'] = "SE";
arrCountryCode['Switzerland'] = "CH";
arrCountryCode['Syria'] = "SY";
arrCountryCode['Taiwan'] = "TW";
arrCountryCode['Tajikistan'] = "TJ";
arrCountryCode['Tanzania'] = "TZ";
arrCountryCode['Thailand'] = "TH";
arrCountryCode['Togo'] = "TG";
arrCountryCode['Tokelau'] = "TK";
arrCountryCode['Tonga'] = "TO";
arrCountryCode['Trinidad And Tobago'] = "TT";
arrCountryCode['Tunisia'] = "TN";
arrCountryCode['Turkey'] = "TR";
arrCountryCode['Turkmenistan'] = "TM";
arrCountryCode['Turks And Caicos Islands'] = "TC";
arrCountryCode['Tuvalu'] = "TV";
arrCountryCode['Uganda'] = "UG";
arrCountryCode['Ukraine'] = "UA";
arrCountryCode['United Arab Emirates'] = "AE";
arrCountryCode['United Kingdom'] = "GB";
arrCountryCode['United States'] = "US";
arrCountryCode['United States Minor Outlying Islands'] = "UM";
arrCountryCode['Uruguay'] = "UY";
arrCountryCode['Uzbekistan'] = "UZ";
arrCountryCode['Vanuatu'] = "VU";
arrCountryCode['Vatican City (Holy See)'] = "VA";
arrCountryCode['Venezuela'] = "VE";
arrCountryCode['Vietnam'] = "VN";
arrCountryCode['Virgin Islands (British)'] = "VG";
arrCountryCode['Virgin Islands (US)'] = "VI";
arrCountryCode['Wallis And Futuna Islands'] = "WF";
arrCountryCode['Western Sahara'] = "EH";
arrCountryCode['Western Samoa'] = "WS";
arrCountryCode['Yemen'] = "YE";
arrCountryCode['Yugoslavia'] = "YU";
arrCountryCode['Zambia'] = "ZM";
arrCountryCode['Zimbabwe'] = "ZW";
// TODO : Do we need to store country name in array while storing? I think we don't need it
// that's why removing this code -- countryName.toUpperCase()
var code = arrCountryCode[countryName];
if (code === undefined) {
return "";
} else {
return code;
}
}
Is it fine? Or there is better way to do it?
Thanks,
Kiran
It is not possible to specify the version to install in the bower.json file since you have not created a git tag for the release version 1.0.0.
It is a part of the release process to create a tag in git with the release version name. See http://bower.io/docs/creating-packages/
Please create the git branch 1.0.0 with
git br 1.0.0
and then push it to github with
git push origin 1.0.0
to fix this issue
After trying to upgrade from webpack 1 to 4 with babel 7, I can't run my application. I'm getting formatLocal is no a function, when I try to use the package as I used to.
after browsing the source files loaded to chrome, I see that phone-format-exports.js but I feels like webpack can't match phoneformat.js import to phone-format-exports.js file.
Did anyone succeed in running phoneformat.js on modern bundle systems?
Thanks!
The documentation does not specify the country code as needing to the 2-digit letter ISO country code (eg "US") and not the numeric country code. It would be great to update the docs to reflect this.
A listing of the appropriate letter-based ISO country codes can be found here:
https://countrycode.org/
This should help others where I got confused. Thanks!
Hi,
Have you noticed that your demo site domain needs renewing so your online demo isn't available?
Great tool btw.
Thanks
Entering the phone number 8646978257
on the phoneformat.com will result in the same number for national and international: (864) 697-8257
I would have expected the number to be: +1 (864) 697-8257
The E164 does output the country code: +18646978257
Hi.
0048666555444 and +48666555444 are valid numbers but for phoneformat.js only +48666555444 is valid.
Regards,
Adam
Seems to be a conspiracy.
This number is false for your rules : +61473448744
Source : https://www.facebook.com/NerrigundahBlueberries/info?tab=page_info
Thanks
It looks like there is something wrong with the grunt build configuration since the API functions seems to be included more than once
e.g. the formatInternational
method can be found on line 978 and line 1968
in phone-format.js
file.
This problem occurs for all other files as well.
This is critical since the files size is quite large (865.901 kb unminimized). It looks like it is twice as large as the google-libraries.js
file.
Hey, I was implementing a test file for phone numbers using protractor in angularjs and came across this error which I thought I should point it out..
error message:
Expected '+1123456' not to be defined.
Error: Expected '+1123456' not to be defined.
code line:
expect(formatE164('US','123456')).not.toBeDefined();
And I think formatE164 should return 'undefined' in this case instead of putting +1 in front of invalid U.S number...
First, thanks much @albeebe for putting this together. Definitely saved me a lot of time.
Quick question: Is there any special reason why we are sanitizing the raw input of non numeric characters with the cleanPhone()
method as opposed to just allowing libphonenumber to determine whether or not the phone number is valid?
One use case where allowing characters in the input would be useful is when you have a number like 1-800-CONTACT (1-800-266-8228) or 1-800-ASK-USPS (1-800-275-8777). Google's libphonenumber actually parses characters using the correct corresponding digit and let's you know if it is valid or not (both valid in this case).
I am validating and then applying local formatting to a field in a form.
If I put a valid German number and the function applies the format, the field turns invalid.
Example:
I input 015733974709, field is marked as valid (according the lib)
The function formatLocal turns it into "01573 3974709"
The validation triggers again and marks the field as invalid.
Can you please add a license file to your project?
In phoneformat.com/js/source.js#L67 you test the phone input as being valid or not which does not take into account the selected country. Wouldn't it be more correct to test the resulting E164 number for this validation?
(great work on this project btw thanks 😃)
I'm using ember! this is the snippet
let value = this.get('value'); // +91876543210!@#$%
if (!isValidNumber(value)) { }
The above number is accepted as valid number. But obviously its not. But in
another eg: +91876543210$%^23 //invalid number
Am I doing it right?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.