Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Parse the phone input into an output format of your choice. Good for formatting a user input.
JavaScript CoffeeScript
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
tests
.gitignore
History.md
Makefile
Readme.md
component.json
index.js
package.json

Readme.md

Phone Parser

Parse the phone input into an output format of your choice. Good for formatting a user input.

Installation

Through component:

$ component install chenglou/phone-parser

Through npm:

$ npm install phone-parser

Or directly download the script for browsers. Rename it however you want.

API

'x' represents a digit placeholder.

// This is the component/npm format. Browser format below.
var phoneParser = require('phone-parser');

phoneParser('2341234447', '(xxx) xxx-xxxx'); // Returns '(234) 123-4447'.
phoneParser('+86 455 6988', 'xxxxxxxxx'); // Returns '864556988'.
phoneParser(' 123 4567  ', '+1 xxx xxxx'); // Returns '+1 123 4567'.
phoneParser('123 4567890', '+1 xxx xxxx'); // Error: phone format cannot be parsed.

Remember, only 'x' is a placeholder. The rest is treated as part of the formatting.

For browsers (real-life example of formatting an input field):

<input type="text" id="phone" />
<script src="index.js"></script>
<script>
  var phoneField = document.getElementById('phone');
  phoneField.addEventListener('change', function() {
    try{
      phoneField.value = phoneParser(phoneField.value, '(xxx) xxx-xxxx');
    } catch(e) {}
  });
</script>

Testing

Using npm:

If you haven't installed jasmine-node and CoffeeScript:

$ npm install

Then:

$ npm test

License

MIT.

Something went wrong with that request. Please try again.