Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/stripe-archive/jquery.mobilePhoneNumber
[DEPRECATED] A general purpose library for validating and formatting mobile phone numbers.
https://github.com/stripe-archive/jquery.mobilePhoneNumber
Last synced: 8 days ago
JSON representation
[DEPRECATED] A general purpose library for validating and formatting mobile phone numbers.
- Host: GitHub
- URL: https://github.com/stripe-archive/jquery.mobilePhoneNumber
- Owner: stripe-archive
- License: mit
- Archived: true
- Created: 2014-05-21T23:24:28.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2020-04-07T20:02:18.000Z (over 4 years ago)
- Last Synced: 2024-08-09T02:37:43.098Z (3 months ago)
- Language: CoffeeScript
- Homepage:
- Size: 97.7 KB
- Stars: 331
- Watchers: 47
- Forks: 46
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Project status
This project is deprecated.
We will patch jquery.mobilePhoneNumber for critical security issues, but won't be adding any new features.
# jQuery.mobilePhoneNumber [![Build Status](https://travis-ci.org/stripe/jquery.mobilePhoneNumber.svg?branch=master)](https://travis-ci.org/stripe/jquery.mobilePhoneNumber) [![CDNJS](https://img.shields.io/cdnjs/v/jquery.mobilephonenumber.svg)](https://cdnjs.com/libraries/jquery.mobilephonenumber/)
A general purpose library for validating and formatting mobile phone numbers.
```javascript
$("input.phone-num").mobilePhoneNumber();
```You can bind to an event when the user changes the country of the phone number:
```javascript
$("input.phone-num").bind("country.mobilePhoneNumber", function(e, country) {
console.log("The new country code:", country);
});
```You can find a [demo here](http://stripe.github.io/jquery.mobilePhoneNumber/example).
Dependencies:
- [jQuery.caret](http://plugins.jquery.com/caret)
- Tested on jQuery 1.8.3 and 1.11.1## API
### \$.fn.mobilePhoneNumber([options])
Enables phone number formatting.
Options:
- `defaultPrefix`: allows the user to type a phone number without the prefix for this specific value.
Example:
```javascript
$("input.phone-num").mobilePhoneNumber({
defaultPrefix: "+1"
});
```### \$.fn.mobilePhoneNumber('val')
Returns the phone number value with prefix, but without other formatting.
Example:
```javascript
$("input.phone-num").val(); //=> '+1 (415) 123-5554'
$("input.phone-num").mobilePhoneNumber("val"); //=> '+14151235554'
```### \$.fn.mobilePhoneNumber('validate')
Returns whether the phone number is valid.
_Note:_ this implementation is very naive; it only validates that the phone number is longer than its prefix.
Example:
```javascript
$("input.phone-num").val(); //=> '+1 (415) 123-5554'
$("input.phone-num").mobilePhoneNumber("validate"); //=> true$("input.phone-num").val(); //=> '+43'
$("input.phone-num").mobilePhoneNumber("validate"); //=> false
```### \$.fn.mobilePhoneNumber('country')
Returns the two-letter country code of the phone number.
Example:
```javascript
$("input.phone-num").val(); //=> '+32 495 12 34 56'
$("input.phone-num").mobilePhoneNumber("country"); //=> 'BE'
```### \$.fn.mobilePhoneNumber('prefix')
Returns the prefix of the phone number.
Example:
```javascript
$("input.phone-num").val(); //=> '+32 495 12 34 56'
$("input.phone-num").mobilePhoneNumber("prefix"); //=> '+32'
```### \$.formatMobilePhoneNumber(phone)
Returns the formatted phone number.
Example:
```javascript
$.formatMobilePhoneNumber("14151235554"); //=> '+1 (415) 123-5554'
```## Events
### country.mobilePhoneNumber
Triggered when the country has changed.
Example:
```javascript
$("input.phone-num").bind("country.mobilePhoneNumber", function(e, country) {
console.log("The new country code:", country);
});// Simulate user input
$("input.phone-num")
.val("+32495123456")
.keyup();
//=> The new country code: BE
```## Building
Run `cake build`
## Running tests
Run `cake test`
## Mobile recommendations
We recommend you set the `pattern`, `type`, and `x-autocompletetype` attributes, which will trigger autocompletion and a numeric keypad to display on touch devices:
```html
```
You may have to turn off HTML5 validation (using the `novalidate` form attribute) when using this `pattern`, since it won't permit spaces and other characters that appear in the formatted version of the phone number.