PhoneNumber.js for the Web is a fork of PhoneNumber.js, a JavaScript library to parse phone numbers. PhoneNumber.js is meant to run mainly on FirefoxOS phones so browser compatibility is not a top priority for it. PhoneNumber.js for the Web tries to fix this by bringing this awesome library even to ancient browsers like Internet Explorer 6. See Usage for details.
You can try it out with your browser here.
PhoneNumber.js is a JavaScript library to verify and format phone numbers. It is similar in purpose to Google's libphonenumber library, with the main difference that Google's code is some incredibly ugly spaghetti code that was cross-compiled from Java and uses around 10MB of memory.
The memory use of PhoneNumber.js starts around 60k and increases as region meta data is unpacked. Depending on the memory layout of the specific JavaScript engine, peak memory use should be below 200k. If you mostly format numbers from one or a few regions, memory use should be pretty close to 60k.
PhoneNumber.js uses libphonenumber's PhoneNumberMetadata.xml database of known phone number formats. Use "grunt" to download the xml file and translate it into PhoneNumber.js's internal format.
PhoneNumber.js for the Web requires grunt-cli to be installed in your system. If you do not have it yet, install it from npm:
npm install -g grunt-cli
Install dependencies and run grunt:
npm install
grunt
Additionally grunt has the following tasks:
- grunt download (default task) - downloads PhoneNumberMetadata.xml and stores it as a JavaScript file
- grunt clean - removes the generated PhoneNumberMetadata.js file
- grunt test - downloads PhoneNumberMetadata.xml and prints converted JavaScript to console
If you want to update PhoneNumberMetadata.js, just run:
grunt
Include the following files in your project
- PhoneNumberMetadata.js (downloaded by
make
) - PhoneNumberNormalizer.js
- PhoneNumber.js
Loading these files exposes the following globals: PhoneNumber
, PhoneNumberNormalizer
and PHONE_NUMBER_META_DATA
.
Parse phone numbers with
PhoneNumber.Parse(dial[, region]) -> Object|false
Where
- dial - phone number to be parsed
- region - 2 letter country code (eg, "US", "CA", etc.)
If the parser is not able to parse provided number, the returned value is false
.
var result = PhoneNumber.Parse("55 555 555", "EE");
Parsed PhoneNumber object has the following properties:
- region - 2 letter country code (if set or detected, ex. "EE")
- regionMetaData.countryCode Prefix (if region was detected, ex. "372")
- nationalNumber - phone number that can be dialled locally (ex. "55555555")
- nationalFormat - text formatted national number (ex. "5555 5555")
- internationalNumber - phone number that can be dialled internationally (ex "+37255555555")
- internationalFormat - text formatted international number (ex. "+372 5555 5555")
Detects if provided string is a numeric only phone number with no text formatting used. This is useful when validating really weird input like "*8" which is a real phone number in Venezuela.
PhoneNumber.IsPlain(number) -> Boolean
Where
- number - phone number that needs checking.
If provided number is a plain number, the returned value is true
PhoneNumber.IsPlain("(1)23"); // false
PhoneNumber.IsPlain("123"); // true
Normalize a number by converting unicode numbers and symbols to their ASCII equivalents and removing all non-dialable characters.
PhoneNumberNormalizer.Normalize(number[, numbersOnly]) -> String
Where
- number - phone number to be normalized
- numbersOnly - if set to true, skips non numeric input
Returned value includes numbers and phone number specific cymbols, like "+", "#", etc.
PhoneNumberNormalizer.Normalize("+123ABC"); // "+123222"
PhoneNumberNormalizer.Normalize("+123ABC", true); // "+123"
Tests reside in the tests folder and are handled by Qunit.
NB! Do not forget to run make
in the root folder of this project before you run the tests first time - the tests rely on PhoneNumberMetadata.js that is generated by make
.
PhoneNumber.js was written by Andreas Gal [email protected] as part of Mozilla's Firefox OS (Boot to Gecko) project and is licensed under the Apache License, Version 2.0.
PhoneNumber.js for the Web is a fork of PhoneNumber.js by Andris Reinman and licensed under the Apache License, Version 2.0.