jQuery.mobilePhoneNumber
A general purpose library for validating and formatting mobile phone numbers.
;
You can bind to an event when the user changes the country of the phone number:
You can find a demo here.
Dependencies:
- jQuery.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:
;
$.fn.mobilePhoneNumber('val')
Returns the phone number value with prefix, but without other formatting.
Example:
; //=> '+1 (415) 123-5554'; //=> '+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:
; //=> '+1 (415) 123-5554'; //=> true; //=> '+43'; //=> false
$.fn.mobilePhoneNumber('country')
Returns the two-letter country code of the phone number.
Example:
; //=> '+32 495 12 34 56'; //=> 'BE'
$.fn.mobilePhoneNumber('prefix')
Returns the prefix of the phone number.
Example:
; //=> '+32 495 12 34 56'; //=> '+32'
$.formatMobilePhoneNumber(phone)
Returns the formatted phone number.
Example:
$; //=> '+1 (415) 123-5554'
Events
country.mobilePhoneNumber
Triggered when the country has changed.
Example:
// Simulate user input;//=> 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:
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.