├── .gitignore
├── .travis.yml
├── bower.json
├── Cakefile
├── package.json
├── mobilephonenumber.jquery.json
├── LICENSE
├── example
└── index.html
├── vendor
└── jquery.caret.js
├── README.md
├── test
└── index.coffee
├── src
└── jquery.mobilePhoneNumber.coffee
└── lib
└── jquery.mobilePhoneNumber.js
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
2 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: node_js
2 | node_js:
3 | - 9
4 |
--------------------------------------------------------------------------------
/bower.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "jquery.mobilephonenumber",
3 | "version": "1.0.6",
4 | "main": "lib/jquery.mobilePhoneNumber.js",
5 | "dependencies": {
6 | "jquery": ">=1.5"
7 | }
8 | }
9 |
--------------------------------------------------------------------------------
/Cakefile:
--------------------------------------------------------------------------------
1 | {spawn} = require 'child_process'
2 | path = require 'path'
3 |
4 | binPath = (bin) -> path.resolve(__dirname, "./node_modules/.bin/#{bin}")
5 |
6 | runExternal = (cmd, args) ->
7 | child = spawn(binPath(cmd), args, stdio: 'inherit')
8 | child.on('error', console.error)
9 | child.on('close', process.exit)
10 |
11 | task 'build', 'Build lib/ from src/', ->
12 | runExternal 'coffee', ['-c', '-o', 'lib', 'src']
13 |
14 | task 'watch', 'Watch src/ for changes', ->
15 | runExternal 'coffee', ['-w', '-c', '-o', 'lib', 'src']
16 |
17 | task 'test', 'Run tests', ->
18 | runExternal 'mocha', ['--require', 'coffeescript/register', 'test/**/*.{js,coffee}']
19 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "jquery.mobilephonenumber",
3 | "version": "1.0.8",
4 | "description": "A general purpose library for validating and formatting mobile phone numbers.",
5 | "license": "MIT",
6 | "keywords": [
7 | "phone"
8 | ],
9 | "author": "Stripe (https://www.stripe.com)",
10 | "repository": {
11 | "type": "git",
12 | "url": "https://github.com/stripe/jquery.mobilePhoneNumber.git"
13 | },
14 | "main": "lib/jquery.mobilePhoneNumber.js",
15 | "scripts": {
16 | "test": "cake test"
17 | },
18 | "//": "nwmatcher is a dep of jquery; pinning version for security issue; ok to remove when upgrading jquery past 3.4.1",
19 | "dependencies": {
20 | "jquery": "^3.4.1",
21 | "nwmatcher": "^1.4.4"
22 | },
23 | "devDependencies": {
24 | "cake": "~0.1",
25 | "coffeescript": "~1.7",
26 | "jsdom": "^11.6.0",
27 | "mocha": "^5.2.0"
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/mobilephonenumber.jquery.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "mobilephonenumber",
3 | "version": "1.0.5",
4 | "title": "jQuery.mobilePhoneNumber",
5 | "description": "A general purpose library for validating and formatting mobile phone numbers.",
6 | "keywords": [
7 | "phone"
8 | ],
9 | "author": {
10 | "name": "Stripe",
11 | "url": "https://www.stripe.com",
12 | "email": "support+github@stripe.com"
13 | },
14 | "licenses": [
15 | {
16 | "type": "MIT",
17 | "url": "https://github.com/stripe/jquery.mobilePhoneNumber/blob/master/LICENSE"
18 | }
19 | ],
20 | "homepage": "https://github.com/stripe/jquery.mobilePhoneNumber",
21 | "docs": "https://github.com/stripe/jquery.mobilePhoneNumber",
22 | "bugs": "https://github.com/stripe/jquery.mobilePhoneNumber/issues",
23 | "demo": "http://stripe.github.io/jquery.mobilePhoneNumber/example",
24 | "dependencies": {
25 | "jquery": ">=1.5",
26 | "caret": ">=1.3.1"
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright (c) 2014 Stripe
2 |
3 | Permission is hereby granted, free of charge, to any person obtaining
4 | a copy of this software and associated documentation files (the
5 | "Software"), to deal in the Software without restriction, including
6 | without limitation the rights to use, copy, modify, merge, publish,
7 | distribute, sublicense, and/or sell copies of the Software, and to
8 | permit persons to whom the Software is furnished to do so, subject to
9 | the following conditions:
10 |
11 | The above copyright notice and this permission notice shall be
12 | included in all copies or substantial portions of the Software.
13 |
14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 |
--------------------------------------------------------------------------------
/example/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Demo (jQuery.mobilePhoneNumber)
5 |
6 |
7 |
8 |
9 |
24 |
25 |
34 |
35 |
36 |
37 |
38 | Demo (jQuery.mobilePhoneNumber)
39 |
40 |
41 |
42 |
43 | Try different phone numbers like:
44 |
45 | - 415 123 4567
46 | - +1 415 123 4567
47 | - +32 495 12 34 56
48 |
49 |
50 | View source code on Github
51 |
52 |
53 |
54 |
--------------------------------------------------------------------------------
/vendor/jquery.caret.js:
--------------------------------------------------------------------------------
1 | (function($) {
2 | $.fn.caret = function(pos) {
3 | var target = this[0];
4 | var isContentEditable = target.contentEditable === 'true';
5 | //get
6 | if (arguments.length == 0) {
7 | //HTML5
8 | if (window.getSelection) {
9 | //contenteditable
10 | if (isContentEditable) {
11 | target.focus();
12 | var range1 = window.getSelection().getRangeAt(0),
13 | range2 = range1.cloneRange();
14 | range2.selectNodeContents(target);
15 | range2.setEnd(range1.endContainer, range1.endOffset);
16 | return range2.toString().length;
17 | }
18 | //textarea
19 | return target.selectionStart;
20 | }
21 | //IE<9
22 | if (document.selection) {
23 | target.focus();
24 | //contenteditable
25 | if (isContentEditable) {
26 | var range1 = document.selection.createRange(),
27 | range2 = document.body.createTextRange();
28 | range2.moveToElementText(target);
29 | range2.setEndPoint('EndToEnd', range1);
30 | return range2.text.length;
31 | }
32 | //textarea
33 | var pos = 0,
34 | range = target.createTextRange(),
35 | range2 = document.selection.createRange().duplicate(),
36 | bookmark = range2.getBookmark();
37 | range.moveToBookmark(bookmark);
38 | while (range.moveStart('character', -1) !== 0) pos++;
39 | return pos;
40 | }
41 | // Addition for jsdom support
42 | if (target.selectionStart)
43 | return target.selectionStart;
44 | //not supported
45 | return 0;
46 | }
47 | //set
48 | if (pos == -1)
49 | pos = this[isContentEditable? 'text' : 'val']().length;
50 | //HTML5
51 | if (window.getSelection) {
52 | //contenteditable
53 | if (isContentEditable) {
54 | target.focus();
55 | window.getSelection().collapse(target.firstChild, pos);
56 | }
57 | //textarea
58 | else
59 | target.setSelectionRange(pos, pos);
60 | }
61 | //IE<9
62 | else if (document.body.createTextRange) {
63 | if (isContentEditable) {
64 | var range = document.body.createTextRange();
65 | range.moveToElementText(target);
66 | range.moveStart('character', pos);
67 | range.collapse(true);
68 | range.select();
69 | } else {
70 | var range = target.createTextRange();
71 | range.move('character', pos);
72 | range.select();
73 | }
74 | }
75 | if (!isContentEditable)
76 | target.focus();
77 | return pos;
78 | }
79 | })(jQuery);
80 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Project status
2 |
3 | This project is deprecated.
4 |
5 | We will patch jquery.mobilePhoneNumber for critical security issues, but won't be adding any new features.
6 |
7 | # jQuery.mobilePhoneNumber [](https://travis-ci.org/stripe/jquery.mobilePhoneNumber) [](https://cdnjs.com/libraries/jquery.mobilephonenumber/)
8 |
9 | A general purpose library for validating and formatting mobile phone numbers.
10 |
11 | ```javascript
12 | $("input.phone-num").mobilePhoneNumber();
13 | ```
14 |
15 | You can bind to an event when the user changes the country of the phone number:
16 |
17 | ```javascript
18 | $("input.phone-num").bind("country.mobilePhoneNumber", function(e, country) {
19 | console.log("The new country code:", country);
20 | });
21 | ```
22 |
23 | You can find a [demo here](http://stripe.github.io/jquery.mobilePhoneNumber/example).
24 |
25 | Dependencies:
26 |
27 | - [jQuery.caret](http://plugins.jquery.com/caret)
28 | - Tested on jQuery 1.8.3 and 1.11.1
29 |
30 | ## API
31 |
32 | ### \$.fn.mobilePhoneNumber([options])
33 |
34 | Enables phone number formatting.
35 |
36 | Options:
37 |
38 | - `defaultPrefix`: allows the user to type a phone number without the prefix for this specific value.
39 |
40 | Example:
41 |
42 | ```javascript
43 | $("input.phone-num").mobilePhoneNumber({
44 | defaultPrefix: "+1"
45 | });
46 | ```
47 |
48 | ### \$.fn.mobilePhoneNumber('val')
49 |
50 | Returns the phone number value with prefix, but without other formatting.
51 |
52 | Example:
53 |
54 | ```javascript
55 | $("input.phone-num").val(); //=> '+1 (415) 123-5554'
56 | $("input.phone-num").mobilePhoneNumber("val"); //=> '+14151235554'
57 | ```
58 |
59 | ### \$.fn.mobilePhoneNumber('validate')
60 |
61 | Returns whether the phone number is valid.
62 |
63 | _Note:_ this implementation is very naive; it only validates that the phone number is longer than its prefix.
64 |
65 | Example:
66 |
67 | ```javascript
68 | $("input.phone-num").val(); //=> '+1 (415) 123-5554'
69 | $("input.phone-num").mobilePhoneNumber("validate"); //=> true
70 |
71 | $("input.phone-num").val(); //=> '+43'
72 | $("input.phone-num").mobilePhoneNumber("validate"); //=> false
73 | ```
74 |
75 | ### \$.fn.mobilePhoneNumber('country')
76 |
77 | Returns the two-letter country code of the phone number.
78 |
79 | Example:
80 |
81 | ```javascript
82 | $("input.phone-num").val(); //=> '+32 495 12 34 56'
83 | $("input.phone-num").mobilePhoneNumber("country"); //=> 'BE'
84 | ```
85 |
86 | ### \$.fn.mobilePhoneNumber('prefix')
87 |
88 | Returns the prefix of the phone number.
89 |
90 | Example:
91 |
92 | ```javascript
93 | $("input.phone-num").val(); //=> '+32 495 12 34 56'
94 | $("input.phone-num").mobilePhoneNumber("prefix"); //=> '+32'
95 | ```
96 |
97 | ### \$.formatMobilePhoneNumber(phone)
98 |
99 | Returns the formatted phone number.
100 |
101 | Example:
102 |
103 | ```javascript
104 | $.formatMobilePhoneNumber("14151235554"); //=> '+1 (415) 123-5554'
105 | ```
106 |
107 | ## Events
108 |
109 | ### country.mobilePhoneNumber
110 |
111 | Triggered when the country has changed.
112 |
113 | Example:
114 |
115 | ```javascript
116 | $("input.phone-num").bind("country.mobilePhoneNumber", function(e, country) {
117 | console.log("The new country code:", country);
118 | });
119 |
120 | // Simulate user input
121 | $("input.phone-num")
122 | .val("+32495123456")
123 | .keyup();
124 | //=> The new country code: BE
125 | ```
126 |
127 | ## Building
128 |
129 | Run `cake build`
130 |
131 | ## Running tests
132 |
133 | Run `cake test`
134 |
135 | ## Mobile recommendations
136 |
137 | We recommend you set the `pattern`, `type`, and `x-autocompletetype` attributes, which will trigger autocompletion and a numeric keypad to display on touch devices:
138 |
139 | ```html
140 |
141 | ```
142 |
143 | 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.
144 |
--------------------------------------------------------------------------------
/test/index.coffee:
--------------------------------------------------------------------------------
1 | assert = require('assert')
2 | JSDOM = require('jsdom').JSDOM
3 | dom = new JSDOM('')
4 | global.document = dom.window.document
5 | global.window = dom.window
6 | $ = require('jquery')
7 | global.jQuery = $
8 |
9 | # jsdom doesn't support selection, so we just hack it into document.createElement
10 | createElement = document.createElement
11 | document.createElement = ->
12 | el = createElement.apply(document, arguments)
13 | if arguments[0] == 'input'
14 | el.selectionStart = el.selectionEnd = 0
15 | el
16 |
17 | require('../src/jquery.mobilePhoneNumber')
18 | require('../vendor/jquery.caret')
19 |
20 | createInput = ->
21 | $input = $('')
22 | # jsdom doesn't support selection, so we just define it
23 | $input[0].selectionStart = $input[0].selectionEnd = 0
24 | $input
25 |
26 | triggerKey = ($input, which) ->
27 | for type in ['keydown', 'keypress', 'keyup']
28 | $input.trigger(
29 | type: type
30 | which: which
31 | )
32 |
33 | type = ($input, digits) ->
34 | for digit in digits
35 | do (digit) ->
36 | triggerKey($input, digit.charCodeAt(0))
37 |
38 | # jsdom doesn't support selection
39 | # hack to push the selection to the end
40 | $input[0].selectionStart = $input[0].selectionEnd = 1000
41 |
42 | describe 'jquery.mobilePhoneNumber', ->
43 | describe 'mobilePhoneNumber', ->
44 | it 'shouldnt change the input value when enabling if value was null', ->
45 | $phone = createInput().val('').mobilePhoneNumber()
46 |
47 | assert.equal $phone.val(), ''
48 |
49 | it 'should correctly format US phone', ->
50 | $phone = createInput().val('').mobilePhoneNumber()
51 |
52 | type $phone, '1'
53 | assert.equal $phone.val(), '+1 ('
54 |
55 | type $phone, '4'
56 | assert.equal $phone.val(), '+1 (4'
57 |
58 | type $phone, '15'
59 | assert.equal $phone.val(), '+1 (415) '
60 |
61 | type $phone, '12'
62 | assert.equal $phone.val(), '+1 (415) 12'
63 |
64 | type $phone, '3'
65 | assert.equal $phone.val(), '+1 (415) 123-'
66 |
67 | type $phone, '4567'
68 | assert.equal $phone.val(), '+1 (415) 123-4567'
69 |
70 | it 'should correctly format US phone with defaultPrefix +1', ->
71 | $phone = createInput().val('').mobilePhoneNumber({ defaultPrefix: '+1' })
72 |
73 | type $phone, '415'
74 | assert.equal $phone.val(), '(415) '
75 |
76 | type $phone, '1234567'
77 | assert.equal $phone.val(), '(415) 123-4567'
78 |
79 | it 'should correctly format JP phone with defaultPrefix +81', ->
80 | $phone = createInput().val('').mobilePhoneNumber({ defaultPrefix: '+81' })
81 |
82 | type $phone, '08043691337'
83 | assert.equal $phone.val(), '080-4369-1337'
84 |
85 | it 'should correctly format BE phone', ->
86 | $phone = createInput().val('').mobilePhoneNumber()
87 |
88 | type $phone, '+32'
89 | assert.equal $phone.val(), '+32 '
90 |
91 | type $phone, '49'
92 | assert.equal $phone.val(), '+32 49'
93 |
94 | type $phone, '5'
95 | assert.equal $phone.val(), '+32 495 '
96 |
97 | type $phone, '1'
98 | assert.equal $phone.val(), '+32 495 1'
99 |
100 | type $phone, '2'
101 | assert.equal $phone.val(), '+32 495 12 '
102 |
103 | type $phone, '3456'
104 | assert.equal $phone.val(), '+32 495 12 34 56'
105 |
106 | it 'should correctly format BE phone with defaultPrefix +1', ->
107 | $phone = createInput().val('').mobilePhoneNumber({ defaultPrefix: '+1' })
108 |
109 | type $phone, '+32'
110 | assert.equal $phone.val(), '+32 '
111 |
112 | type $phone, '123456789'
113 | assert.equal $phone.val(), '+32 123 45 67 89'
114 |
115 | it 'should correctly replace when select all + type', ->
116 | $phone = createInput().val('123456789').mobilePhoneNumber()
117 |
118 | $phone.get(0).selectionStart = 0
119 | $phone.get(0).selectionEnd = 20
120 |
121 | type $phone, '0'
122 |
123 | assert.equal $phone.val(), '+0'
124 |
125 | it 'should correctly format the current value before typing', ->
126 | $phone = createInput().val('4151234567').mobilePhoneNumber({ defaultPrefix: '+1' })
127 | assert.equal $phone.val(), '(415) 123-4567'
128 |
129 | describe 'mobilePhoneNumber("country")', ->
130 | it 'should correctly find the country', ->
131 | $phone = createInput().mobilePhoneNumber()
132 |
133 | $phone.val('+1415123')
134 | assert.equal $phone.mobilePhoneNumber('country'), 'US'
135 |
136 | $phone.val('+3212345')
137 | assert.equal $phone.mobilePhoneNumber('country'), 'BE'
138 |
139 | $phone.val('+3312345')
140 | assert.equal $phone.mobilePhoneNumber('country'), 'FR'
141 |
142 | $phone.val('+1403123')
143 | assert.equal $phone.mobilePhoneNumber('country'), 'CA'
144 |
145 | describe 'mobilePhoneNumber("prefix")', ->
146 | it 'should correctly find the prefix', ->
147 | $phone = createInput().mobilePhoneNumber()
148 |
149 | $phone.val('+1415123')
150 | assert.equal $phone.mobilePhoneNumber('prefix'), '+1'
151 |
152 | $phone.val('+3212345')
153 | assert.equal $phone.mobilePhoneNumber('prefix'), '+32'
154 |
155 | $phone.val('+3312345')
156 | assert.equal $phone.mobilePhoneNumber('prefix'), '+33'
157 |
158 | $phone.val('+1403123')
159 | assert.equal $phone.mobilePhoneNumber('prefix'), '+1'
160 |
161 | describe 'mobilePhoneNumber("val")', ->
162 | it 'should correctly returns the val with defaultPrefix on', ->
163 | $phone = createInput().mobilePhoneNumber({defaultPrefix: '+1'})
164 |
165 | $phone.val('4151234567')
166 | assert.equal $phone.mobilePhoneNumber('val'), '+14151234567'
167 |
168 | $phone.val('+32123456789')
169 | assert.equal $phone.mobilePhoneNumber('val'), '+32123456789'
170 |
171 | it 'should correctly returns the val with defaultPrefix off', ->
172 | $phone = createInput().mobilePhoneNumber()
173 |
174 | $phone.val('+14151234567')
175 | assert.equal $phone.mobilePhoneNumber('val'), '+14151234567'
176 |
177 | $phone.val('+32123456789')
178 | assert.equal $phone.mobilePhoneNumber('val'), '+32123456789'
179 |
180 | describe 'event country.mobilePhoneNumber', ->
181 | it 'is triggered correctly with US number', (done) ->
182 | $phone = createInput().val('').mobilePhoneNumber()
183 | $phone.bind('country.mobilePhoneNumber', (e, country) ->
184 | if country == 'US'
185 | done()
186 | )
187 | type $phone, '+1415'
188 |
189 | it 'is triggered correctly with BE number and then US number', (done) ->
190 | $phone = createInput().val('').mobilePhoneNumber()
191 | isFirst = true
192 | $phone.bind('country.mobilePhoneNumber', (e, country) ->
193 | if isFirst
194 | if country == 'BE'
195 | isFirst = false
196 | else
197 | if country == 'US'
198 | done()
199 | )
200 | type $phone, '+32495'
201 | $phone.val('')
202 | type $phone, '+1415'
203 |
--------------------------------------------------------------------------------
/src/jquery.mobilePhoneNumber.coffee:
--------------------------------------------------------------------------------
1 | $ = jQuery
2 |
3 | supportSelectionEnd = 'selectionEnd' of document.createElement('input')
4 |
5 | # Timeout to work around cursor bugs in Android Firefox
6 | withTimeout = (fn) -> setTimeout(fn, 50)
7 |
8 | formatForPhone_ = (phone, defaultPrefix = null) ->
9 | if phone.indexOf('+') != 0 and defaultPrefix
10 | phone = defaultPrefix + phone.replace(/[^0-9]/g, '')
11 | else
12 | phone = '+' + phone.replace(/[^0-9]/g, '')
13 | bestFormat = null
14 | precision = 0
15 | for prefix, format of formats
16 | if phone.length >= prefix.length && phone.substring(0, prefix.length) == prefix && prefix.length > precision
17 | bestFormat = {}
18 | for k, v of format
19 | bestFormat[k] = v
20 | bestFormat.prefix = prefix
21 | precision = prefix.length
22 | bestFormat
23 |
24 | prefixesAreSubsets_ = (prefixA, prefixB) ->
25 | return true if prefixA == prefixB
26 | if prefixA.length < prefixB.length
27 | return prefixB.substring(0, prefixA.length) == prefixA
28 | return prefixA.substring(0, prefixB.length) == prefixB
29 |
30 | formattedPhoneNumber_ = (phone, lastChar, defaultPrefix = null) ->
31 | if phone.length != 0 and (phone.substring(0, 1) == "+" or defaultPrefix)
32 | format = formatForPhone_(phone, defaultPrefix)
33 | if format && format.format
34 | phoneFormat = format.format
35 | phonePrefix = format.prefix
36 |
37 | if defaultPrefix
38 | if (defaultPrefix == phonePrefix or prefixesAreSubsets_(phonePrefix, defaultPrefix)) and (phone.indexOf('+') != 0 or phone.length == 0)
39 | phoneFormat = phoneFormat.substring(Math.min(phonePrefix.length, defaultPrefix.length) + 1)
40 | if format.nationalPrefix?
41 | prefixPhoneFormat = ""
42 | for i in [0...format.nationalPrefix.length]
43 | prefixPhoneFormat += "."
44 | phoneFormat = prefixPhoneFormat + phoneFormat
45 |
46 | if phone.substring(0, 1) == "+"
47 | phoneDigits = phone.substring(1)
48 | else
49 | phoneDigits = phone
50 | formatDigitCount = phoneFormat.match(/\./g).length
51 |
52 | formattedPhone = ""
53 | for formatChar in phoneFormat
54 | if formatChar == "."
55 | if phoneDigits.length == 0
56 | break
57 | formattedPhone += phoneDigits.substring(0, 1)
58 | phoneDigits = phoneDigits.substring(1)
59 | else if lastChar || phoneDigits.length > 0
60 | formattedPhone += formatChar
61 | phone = formattedPhone + phoneDigits
62 | phone
63 |
64 | isEventAllowed_ = (e) ->
65 | # Key event is for a browser shortcut
66 | return true if e.metaKey
67 | # If keycode is a space
68 | return false if e.which is 32
69 | # If keycode is a special char (WebKit)
70 | return true if e.which is 0
71 | # If char is a special char (Firefox)
72 | return true if e.which < 33
73 | isEventAllowedChar_(e)
74 |
75 | isEventAllowedChar_ = (e) ->
76 | char = String.fromCharCode(e.which)
77 | # Char is a number or a space or a +
78 | !!/[\d\s+]/.test(char)
79 |
80 | restrictEventAndFormat_ = (e) ->
81 | if !isEventAllowed_(e)
82 | return e.preventDefault()
83 |
84 | return if !isEventAllowedChar_(e)
85 | value = @val()
86 | caretEnd = if supportSelectionEnd then @get(0).selectionEnd else @caret()
87 | value = value.substring(0, @caret()) +
88 | String.fromCharCode(e.which) +
89 | value.substring(caretEnd, value.length)
90 | format_.call(@, value, e)
91 | withTimeout => @caret(@val().length)
92 |
93 | formatUp_ = (e) ->
94 | checkForCountryChange_.call(@)
95 | value = @val()
96 | return if e.keyCode == 8 && @caret() == value.length
97 | format_.call(@, value, e)
98 |
99 | formatBack_ = (e) ->
100 | return if !e
101 | return if e.meta
102 | value = @val()
103 | return if value.length == 0
104 | return if !(@caret() == value.length)
105 | return if e.keyCode != 8
106 |
107 | value = value.substring(0, value.length - 1)
108 | e.preventDefault()
109 | phone = formattedPhone_.call(@, value, false)
110 | if @val() != phone
111 | @val(phone)
112 |
113 | format_ = (value, e) ->
114 | phone = formattedPhone_.call(@, value, true)
115 | if phone != @val()
116 | selection = @caret()
117 | selectionAtEnd = selection == @val().length
118 | e.preventDefault()
119 | @val(phone)
120 | if !selectionAtEnd
121 | withTimeout => @caret(selection)
122 |
123 | formattedPhone_ = (phone, lastChar) ->
124 | if phone.indexOf('+') != 0 && @data('defaultPrefix')
125 | phone = phone.replace(/[^0-9]/g, '')
126 | else
127 | phone = '+' + phone.replace(/[^0-9]/g, '')
128 | formattedPhoneNumber_(phone, lastChar, @data('defaultPrefix'))
129 |
130 | checkForCountryChange_ = ->
131 | phone = @val()
132 | format = formatForPhone_(phone, @data('defaultPrefix'))
133 | country = null
134 | country = format.country if format
135 | if @data('mobilePhoneCountry') != country
136 | @data('mobilePhoneCountry', country)
137 | @trigger('country.mobilePhoneNumber', country)
138 |
139 | mobilePhoneNumber = {}
140 | mobilePhoneNumber.init = (options = {}) ->
141 | unless @data('mobilePhoneNumberInited')
142 | @data('mobilePhoneNumberInited', true)
143 | @bind 'keypress', =>
144 | restrictEventAndFormat_.apply($(@), arguments)
145 | @bind 'keyup', =>
146 | formatUp_.apply($(@), arguments)
147 | @bind 'keydown', =>
148 | formatBack_.apply($(@), arguments)
149 |
150 | @data('defaultPrefix', options.allowPhoneWithoutPrefix ? options.defaultPrefix)
151 | if @val() != ''
152 | @val(formattedPhone_.call(this, @val(), false))
153 | @
154 |
155 | mobilePhoneNumber.val = ->
156 | val = @val().replace(/[^0-9]/g, '')
157 | format = formatForPhone_(val, @data('defaultPrefix'))
158 | if @val().indexOf('+') == 0 or !@data('defaultPrefix')?
159 | '+' + val
160 | else
161 | @data('defaultPrefix') + val
162 |
163 | mobilePhoneNumber.validate = ->
164 | val = @mobilePhoneNumber('val')
165 | format = formatForPhone_(val, @data('defaultPrefix'))
166 | return true unless format
167 | return val.length > format.prefix.length
168 |
169 | mobilePhoneNumber.country = ->
170 | format = formatForPhone_(@mobilePhoneNumber('val'))
171 | format.country if format
172 |
173 | mobilePhoneNumber.prefix = ->
174 | countryCode = @mobilePhoneNumber('country')
175 | return "" if !countryCode?
176 | $.mobilePhoneNumberPrefixFromCountryCode(countryCode)
177 |
178 | $.fn.mobilePhoneNumber = (method, args...) ->
179 | if !method? or !(typeof(method) == 'string')
180 | args = [ method ] if method?
181 | method = 'init'
182 | mobilePhoneNumber[method].apply(this, args)
183 |
184 | $.formatMobilePhoneNumber = (phone) ->
185 | phone = '+' + phone.replace(/[^0-9\*]/g, '')
186 | formattedPhoneNumber_(phone, true)
187 |
188 | $.mobilePhoneNumberPrefixFromCountryCode = (countryCode) ->
189 | for prefix, format of formats
190 | if format.country.toLowerCase() == countryCode.toLowerCase()
191 | if prefix.length == 5 and prefix[1] == '1'
192 | return '+1'
193 | return prefix
194 | return null
195 |
196 | formats =
197 | '+247' :
198 | country : 'AC',
199 | '+376' :
200 | country : 'AD',
201 | format : '+... ... ...',
202 | '+971' :
203 | country : 'AE',
204 | format : '+... .. ... ....',
205 | '+93' :
206 | country : 'AF',
207 | format : '+.. .. ... ....',
208 | '+1268' :
209 | country : 'AG',
210 | '+1264' :
211 | country : 'AI',
212 | '+355' :
213 | country : 'AL',
214 | format : '+... .. ... ....',
215 | '+374' :
216 | country : 'AM',
217 | format : '+... .. ......',
218 | '+244' :
219 | country : 'AO',
220 | format : '+... ... ... ...',
221 | '+54' :
222 | country : 'AR',
223 | format : '+.. .. ..-....-....',
224 | '+1684' :
225 | country : 'AS',
226 | '+43' :
227 | country : 'AT',
228 | format : '+.. ... ......',
229 | '+61' :
230 | country : 'AU',
231 | format : '+.. ... ... ...',
232 | '+297' :
233 | country : 'AW',
234 | format : '+... ... ....',
235 | '+994' :
236 | country : 'AZ',
237 | format : '+... .. ... .. ..',
238 | '+387' :
239 | country : 'BA',
240 | format : '+... .. ...-...',
241 | '+1246' :
242 | country : 'BB',
243 | '+880' :
244 | country : 'BD',
245 | format : '+... ....-......',
246 | '+32' :
247 | country : 'BE',
248 | format : '+.. ... .. .. ..',
249 | '+226' :
250 | country : 'BF',
251 | format : '+... .. .. .. ..',
252 | '+359' :
253 | country : 'BG',
254 | format : '+... ... ... ..',
255 | '+973' :
256 | country : 'BH',
257 | format : '+... .... ....',
258 | '+257' :
259 | country : 'BI',
260 | format : '+... .. .. .. ..',
261 | '+229' :
262 | country : 'BJ',
263 | format : '+... .. .. .. ..',
264 | '+1441' :
265 | country : 'BM',
266 | '+673' :
267 | country : 'BN',
268 | format : '+... ... ....',
269 | '+591' :
270 | country : 'BO',
271 | format : '+... ........',
272 | '+55' :
273 | country : 'BR',
274 | format : '+.. .. .....-....',
275 | '+1242' :
276 | country : 'BS',
277 | '+975' :
278 | country : 'BT',
279 | format : '+... .. .. .. ..',
280 | '+267' :
281 | country : 'BW',
282 | format : '+... .. ... ...',
283 | '+375' :
284 | country : 'BY',
285 | format : '+... .. ...-..-..',
286 | '+501' :
287 | country : 'BZ',
288 | format : '+... ...-....',
289 | '+243' :
290 | country : 'CD',
291 | format : '+... ... ... ...',
292 | '+236' :
293 | country : 'CF',
294 | format : '+... .. .. .. ..',
295 | '+242' :
296 | country : 'CG',
297 | format : '+... .. ... ....',
298 | '+41' :
299 | country : 'CH',
300 | format : '+.. .. ... .. ..',
301 | '+225' :
302 | country : 'CI',
303 | format : '+... .. .. .. ..',
304 | '+682' :
305 | country : 'CK',
306 | format : '+... .. ...',
307 | '+56' :
308 | country : 'CL',
309 | format : '+.. . .... ....',
310 | '+237' :
311 | country : 'CM',
312 | format : '+... .. .. .. ..',
313 | '+86' :
314 | country : 'CN',
315 | format : '+.. ... .... ....',
316 | '+57' :
317 | country : 'CO',
318 | format : '+.. ... .......',
319 | '+506' :
320 | country : 'CR',
321 | format : '+... .... ....',
322 | '+53' :
323 | country : 'CU',
324 | format : '+.. . .......',
325 | '+238' :
326 | country : 'CV',
327 | format : '+... ... .. ..',
328 | '+599' :
329 | country : 'CW',
330 | format : '+... . ... ....',
331 | '+537' :
332 | country : 'CY',
333 | '+357' :
334 | country : 'CY',
335 | format : '+... .. ......',
336 | '+420' :
337 | country : 'CZ',
338 | format : '+... ... ... ...',
339 | '+49' :
340 | country : 'DE',
341 | format : '+.. .... .......',
342 | '+253' :
343 | country : 'DJ',
344 | format : '+... .. .. .. ..',
345 | '+45' :
346 | country : 'DK',
347 | format : '+.. .. .. .. ..',
348 | '+1767' :
349 | country : 'DM',
350 | '+1849' :
351 | country : 'DO',
352 | '+213' :
353 | country : 'DZ',
354 | format : '+... ... .. .. ..',
355 | '+593' :
356 | country : 'EC',
357 | format : '+... .. ... ....',
358 | '+372' :
359 | country : 'EE',
360 | format : '+... .... ....',
361 | '+20' :
362 | country : 'EG',
363 | format : '+.. ... ... ....',
364 | '+291' :
365 | country : 'ER',
366 | format : '+... . ... ...',
367 | '+34' :
368 | country : 'ES',
369 | format : '+.. ... .. .. ..',
370 | '+251' :
371 | country : 'ET',
372 | format : '+... .. ... ....',
373 | '+358' :
374 | country : 'FI',
375 | format : '+... .. ... .. ..',
376 | '+679' :
377 | country : 'FJ',
378 | format : '+... ... ....',
379 | '+500' :
380 | country : 'FK',
381 | '+691' :
382 | country : 'FM',
383 | format : '+... ... ....',
384 | '+298' :
385 | country : 'FO',
386 | format : '+... ......',
387 | '+33' :
388 | country : 'FR',
389 | format : '+.. . .. .. .. ..',
390 | '+241' :
391 | country : 'GA',
392 | format : '+... .. .. .. ..',
393 | '+44' :
394 | country : 'GB',
395 | format : '+.. .... ......',
396 | '+1473' :
397 | country : 'GD',
398 | '+995' :
399 | country : 'GE',
400 | format : '+... ... .. .. ..',
401 | '+594' :
402 | country : 'GF',
403 | format : '+... ... .. .. ..',
404 | '+233' :
405 | country : 'GH',
406 | format : '+... .. ... ....',
407 | '+350' :
408 | country : 'GI',
409 | format : '+... ... .....',
410 | '+299' :
411 | country : 'GL',
412 | format : '+... .. .. ..',
413 | '+220' :
414 | country : 'GM',
415 | format : '+... ... ....',
416 | '+224' :
417 | country : 'GN',
418 | format : '+... ... .. .. ..',
419 | '+240' :
420 | country : 'GQ',
421 | format : '+... ... ... ...',
422 | '+30' :
423 | country : 'GR',
424 | format : '+.. ... ... ....',
425 | '+502' :
426 | country : 'GT',
427 | format : '+... .... ....',
428 | '+1671' :
429 | country : 'GU',
430 | '+245' :
431 | country : 'GW',
432 | format : '+... ... ....',
433 | '+592' :
434 | country : 'GY',
435 | format : '+... ... ....',
436 | '+852' :
437 | country : 'HK',
438 | format : '+... .... ....',
439 | '+504' :
440 | country : 'HN',
441 | format : '+... ....-....',
442 | '+385' :
443 | country : 'HR',
444 | format : '+... .. ... ....',
445 | '+509' :
446 | country : 'HT',
447 | format : '+... .. .. ....',
448 | '+36' :
449 | country : 'HU',
450 | format : '+.. .. ... ....',
451 | '+62' :
452 | country : 'ID',
453 | format : '+.. ...-...-...',
454 | '+353' :
455 | country : 'IE',
456 | format : '+... .. ... ....',
457 | '+972' :
458 | country : 'IL',
459 | format : '+... ..-...-....',
460 | '+91' :
461 | country : 'IN',
462 | format : '+.. .. .. ......',
463 | '+246' :
464 | country : 'IO',
465 | format : '+... ... ....',
466 | '+964' :
467 | country : 'IQ',
468 | format : '+... ... ... ....',
469 | '+98' :
470 | country : 'IR',
471 | format : '+.. ... ... ....',
472 | '+354' :
473 | country : 'IS',
474 | format : '+... ... ....',
475 | '+39' :
476 | country : 'IT',
477 | format : '+.. .. .... ....',
478 | '+1876' :
479 | country : 'JM',
480 | '+962' :
481 | country : 'JO',
482 | format : '+... . .... ....',
483 | '+81' :
484 | country : 'JP',
485 | format : '+.. ..-....-....',
486 | nationalPrefix: '0',
487 | '+254' :
488 | country : 'KE',
489 | format : '+... .. .......',
490 | '+996' :
491 | country : 'KG',
492 | format : '+... ... ... ...',
493 | '+855' :
494 | country : 'KH',
495 | format : '+... .. ... ...',
496 | '+686' :
497 | country : 'KI',
498 | '+269' :
499 | country : 'KM',
500 | format : '+... ... .. ..',
501 | '+1869' :
502 | country : 'KN',
503 | '+850' :
504 | country : 'KP',
505 | format : '+... ... ... ....',
506 | '+82' :
507 | country : 'KR',
508 | format : '+.. ..-....-....',
509 | '+965' :
510 | country : 'KW',
511 | format : '+... ... .....',
512 | '+345' :
513 | country : 'KY',
514 | '+77' :
515 | country : 'KZ',
516 | '+856' :
517 | country : 'LA',
518 | format : '+... .. .. ... ...',
519 | '+961' :
520 | country : 'LB',
521 | format : '+... .. ... ...',
522 | '+1758' :
523 | country : 'LC',
524 | '+423' :
525 | country : 'LI',
526 | format : '+... ... ... ...',
527 | '+94' :
528 | country : 'LK',
529 | format : '+.. .. . ......',
530 | '+231' :
531 | country : 'LR',
532 | format : '+... ... ... ...',
533 | '+266' :
534 | country : 'LS',
535 | format : '+... .... ....',
536 | '+370' :
537 | country : 'LT',
538 | format : '+... ... .....',
539 | '+352' :
540 | country : 'LU',
541 | format : '+... .. .. .. ...',
542 | '+371' :
543 | country : 'LV',
544 | format : '+... .. ... ...',
545 | '+218' :
546 | country : 'LY',
547 | format : '+... ..-.......',
548 | '+212' :
549 | country : 'MA',
550 | format : '+... ...-......',
551 | '+377' :
552 | country : 'MC',
553 | format : '+... . .. .. .. ..',
554 | '+373' :
555 | country : 'MD',
556 | format : '+... ... .. ...',
557 | '+382' :
558 | country : 'ME',
559 | format : '+... .. ... ...',
560 | '+590' :
561 | country : 'MF',
562 | '+261' :
563 | country : 'MG',
564 | format : '+... .. .. ... ..',
565 | '+692' :
566 | country : 'MH',
567 | format : '+... ...-....',
568 | '+389' :
569 | country : 'MK',
570 | format : '+... .. ... ...',
571 | '+223' :
572 | country : 'ML',
573 | format : '+... .. .. .. ..',
574 | '+95' :
575 | country : 'MM',
576 | format : '+.. . ... ....',
577 | '+976' :
578 | country : 'MN',
579 | format : '+... .... ....',
580 | '+853' :
581 | country : 'MO',
582 | format : '+... .... ....',
583 | '+1670' :
584 | country : 'MP',
585 | '+596' :
586 | country : 'MQ',
587 | format : '+... ... .. .. ..',
588 | '+222' :
589 | country : 'MR',
590 | format : '+... .. .. .. ..',
591 | '+1664' :
592 | country : 'MS',
593 | '+356' :
594 | country : 'MT',
595 | format : '+... .... ....',
596 | '+230' :
597 | country : 'MU',
598 | format : '+... .... ....',
599 | '+960' :
600 | country : 'MV',
601 | format : '+... ...-....',
602 | '+265' :
603 | country : 'MW',
604 | format : '+... ... .. .. ..',
605 | '+52' :
606 | country : 'MX',
607 | format : '+.. ... ... ... ....',
608 | '+60' :
609 | country : 'MY',
610 | format : '+.. ..-... ....',
611 | '+258' :
612 | country : 'MZ',
613 | format : '+... .. ... ....',
614 | '+264' :
615 | country : 'NA',
616 | format : '+... .. ... ....',
617 | '+687' :
618 | country : 'NC',
619 | format : '+... ........',
620 | '+227' :
621 | country : 'NE',
622 | format : '+... .. .. .. ..',
623 | '+672' :
624 | country : 'NF',
625 | format : '+... .. ....',
626 | '+234' :
627 | country : 'NG',
628 | format : '+... ... ... ....',
629 | '+505' :
630 | country : 'NI',
631 | format : '+... .... ....',
632 | '+31' :
633 | country : 'NL',
634 | format : '+.. . ........',
635 | '+47' :
636 | country : 'NO',
637 | format : '+.. ... .. ...',
638 | '+977' :
639 | country : 'NP',
640 | format : '+... ...-.......',
641 | '+674' :
642 | country : 'NR',
643 | format : '+... ... ....',
644 | '+683' :
645 | country : 'NU',
646 | '+64' :
647 | country : 'NZ',
648 | format : '+.. .. ... ....',
649 | '+968' :
650 | country : 'OM',
651 | format : '+... .... ....',
652 | '+507' :
653 | country : 'PA',
654 | format : '+... ....-....',
655 | '+51' :
656 | country : 'PE',
657 | format : '+.. ... ... ...',
658 | '+689' :
659 | country : 'PF',
660 | format : '+... .. .. ..',
661 | '+675' :
662 | country : 'PG',
663 | format : '+... ... ....',
664 | '+63' :
665 | country : 'PH',
666 | format : '+.. .... ......',
667 | '+92' :
668 | country : 'PK',
669 | format : '+.. ... .......',
670 | '+48' :
671 | country : 'PL',
672 | format : '+.. .. ... .. ..',
673 | '+508' :
674 | country : 'PM',
675 | format : '+... .. .. ..',
676 | '+872' :
677 | country : 'PN',
678 | '+1939' :
679 | country : 'PR',
680 | '+970' :
681 | country : 'PS',
682 | format : '+... ... ... ...',
683 | '+351' :
684 | country : 'PT',
685 | format : '+... ... ... ...',
686 | '+680' :
687 | country : 'PW',
688 | format : '+... ... ....',
689 | '+595' :
690 | country : 'PY',
691 | format : '+... .. .......',
692 | '+974' :
693 | country : 'QA',
694 | format : '+... .... ....',
695 | '+262' :
696 | country : 'RE',
697 | '+40' :
698 | country : 'RO',
699 | format : '+.. .. ... ....',
700 | '+381' :
701 | country : 'RS',
702 | format : '+... .. .......',
703 | '+7' :
704 | country : 'RU',
705 | format : '+. ... ...-..-..',
706 | '+250' :
707 | country : 'RW',
708 | format : '+... ... ... ...',
709 | '+966' :
710 | country : 'SA',
711 | format : '+... .. ... ....',
712 | '+677' :
713 | country : 'SB',
714 | format : '+... ... ....',
715 | '+248' :
716 | country : 'SC',
717 | format : '+... . ... ...',
718 | '+249' :
719 | country : 'SD',
720 | format : '+... .. ... ....',
721 | '+46' :
722 | country : 'SE',
723 | format : '+.. ..-... .. ..',
724 | '+65' :
725 | country : 'SG',
726 | format : '+.. .... ....',
727 | '+290' :
728 | country : 'SH',
729 | '+386' :
730 | country : 'SI',
731 | format : '+... .. ... ...',
732 | '+421' :
733 | country : 'SK',
734 | format : '+... ... ... ...',
735 | '+232' :
736 | country : 'SL',
737 | format : '+... .. ......',
738 | '+378' :
739 | country : 'SM',
740 | format : '+... .. .. .. ..',
741 | '+221' :
742 | country : 'SN',
743 | format : '+... .. ... .. ..',
744 | '+252' :
745 | country : 'SO',
746 | format : '+... .. .......',
747 | '+597' :
748 | country : 'SR',
749 | format : '+... ...-....',
750 | '+211' :
751 | country : 'SS',
752 | format : '+... ... ... ...',
753 | '+239' :
754 | country : 'ST',
755 | format : '+... ... ....',
756 | '+503' :
757 | country : 'SV',
758 | format : '+... .... ....',
759 | '+963' :
760 | country : 'SY',
761 | format : '+... ... ... ...',
762 | '+268' :
763 | country : 'SZ',
764 | format : '+... .... ....',
765 | '+1649' :
766 | country : 'TC',
767 | '+235' :
768 | country : 'TD',
769 | format : '+... .. .. .. ..',
770 | '+228' :
771 | country : 'TG',
772 | format : '+... .. .. .. ..',
773 | '+66' :
774 | country : 'TH',
775 | format : '+.. .. ... ....',
776 | '+992' :
777 | country : 'TJ',
778 | format : '+... ... .. ....',
779 | '+690' :
780 | country : 'TK',
781 | '+670' :
782 | country : 'TL',
783 | format : '+... .... ....',
784 | '+993' :
785 | country : 'TM',
786 | format : '+... .. ..-..-..',
787 | '+216' :
788 | country : 'TN',
789 | format : '+... .. ... ...',
790 | '+676' :
791 | country : 'TO',
792 | format : '+... ... ....',
793 | '+90' :
794 | country : 'TR',
795 | format : '+.. ... ... ....',
796 | '+1868' :
797 | country : 'TT',
798 | '+688' :
799 | country : 'TV',
800 | '+886' :
801 | country : 'TW',
802 | format : '+... ... ... ...',
803 | '+255' :
804 | country : 'TZ',
805 | format : '+... ... ... ...',
806 | '+380' :
807 | country : 'UA',
808 | format : '+... .. ... ....',
809 | '+256' :
810 | country : 'UG',
811 | format : '+... ... ......',
812 | '+1' :
813 | country : 'US',
814 | '+598' :
815 | country : 'UY',
816 | format : '+... .... ....',
817 | '+998' :
818 | country : 'UZ',
819 | format : '+... .. ... .. ..',
820 | '+379' :
821 | country : 'VA',
822 | '+1784' :
823 | country : 'VC',
824 | '+58' :
825 | country : 'VE',
826 | format : '+.. ...-.......',
827 | '+1284' :
828 | country : 'VG',
829 | '+1340' :
830 | country : 'VI',
831 | '+84' :
832 | country : 'VN',
833 | format : '+.. .. ... .. ..',
834 | '+678' :
835 | country : 'VU',
836 | format : '+... ... ....',
837 | '+681' :
838 | country : 'WF',
839 | format : '+... .. .. ..',
840 | '+685' :
841 | country : 'WS',
842 | '+967' :
843 | country : 'YE',
844 | format : '+... ... ... ...',
845 | '+27' :
846 | country : 'ZA',
847 | format : '+.. .. ... ....',
848 | '+260' :
849 | country : 'ZM',
850 | format : '+... .. .......',
851 | '+263' :
852 | country : 'ZW',
853 | format : '+... .. ... ....',
854 |
855 | do (formats) ->
856 | # Canada
857 | canadaPrefixes = [403, 587, 780, 250, 604, 778, 204, 506, 709, 902, 226, 249, 289, 343, 416, 519, 613, 647, 705, 807, 905, 418, 438, 450, 514, 579, 581, 819, 873, 306, 867]
858 | for prefix in canadaPrefixes
859 | formats['+1' + prefix] = { country: 'CA' }
860 |
861 | for prefix, format of formats
862 | if prefix.substring(0, 2) == "+1"
863 | format.format = '+. (...) ...-....'
864 |
--------------------------------------------------------------------------------
/lib/jquery.mobilePhoneNumber.js:
--------------------------------------------------------------------------------
1 | // Generated by CoffeeScript 1.7.1
2 | (function() {
3 | var $, checkForCountryChange_, formatBack_, formatForPhone_, formatUp_, format_, formats, formattedPhoneNumber_, formattedPhone_, isEventAllowedChar_, isEventAllowed_, mobilePhoneNumber, prefixesAreSubsets_, restrictEventAndFormat_, supportSelectionEnd, withTimeout,
4 | __slice = [].slice;
5 |
6 | $ = jQuery;
7 |
8 | supportSelectionEnd = 'selectionEnd' in document.createElement('input');
9 |
10 | withTimeout = function(fn) {
11 | return setTimeout(fn, 50);
12 | };
13 |
14 | formatForPhone_ = function(phone, defaultPrefix) {
15 | var bestFormat, format, k, precision, prefix, v;
16 | if (defaultPrefix == null) {
17 | defaultPrefix = null;
18 | }
19 | if (phone.indexOf('+') !== 0 && defaultPrefix) {
20 | phone = defaultPrefix + phone.replace(/[^0-9]/g, '');
21 | } else {
22 | phone = '+' + phone.replace(/[^0-9]/g, '');
23 | }
24 | bestFormat = null;
25 | precision = 0;
26 | for (prefix in formats) {
27 | format = formats[prefix];
28 | if (phone.length >= prefix.length && phone.substring(0, prefix.length) === prefix && prefix.length > precision) {
29 | bestFormat = {};
30 | for (k in format) {
31 | v = format[k];
32 | bestFormat[k] = v;
33 | }
34 | bestFormat.prefix = prefix;
35 | precision = prefix.length;
36 | }
37 | }
38 | return bestFormat;
39 | };
40 |
41 | prefixesAreSubsets_ = function(prefixA, prefixB) {
42 | if (prefixA === prefixB) {
43 | return true;
44 | }
45 | if (prefixA.length < prefixB.length) {
46 | return prefixB.substring(0, prefixA.length) === prefixA;
47 | }
48 | return prefixA.substring(0, prefixB.length) === prefixB;
49 | };
50 |
51 | formattedPhoneNumber_ = function(phone, lastChar, defaultPrefix) {
52 | var format, formatChar, formatDigitCount, formattedPhone, i, phoneDigits, phoneFormat, phonePrefix, prefixPhoneFormat, _i, _j, _len, _ref;
53 | if (defaultPrefix == null) {
54 | defaultPrefix = null;
55 | }
56 | if (phone.length !== 0 && (phone.substring(0, 1) === "+" || defaultPrefix)) {
57 | format = formatForPhone_(phone, defaultPrefix);
58 | if (format && format.format) {
59 | phoneFormat = format.format;
60 | phonePrefix = format.prefix;
61 | if (defaultPrefix) {
62 | if ((defaultPrefix === phonePrefix || prefixesAreSubsets_(phonePrefix, defaultPrefix)) && (phone.indexOf('+') !== 0 || phone.length === 0)) {
63 | phoneFormat = phoneFormat.substring(Math.min(phonePrefix.length, defaultPrefix.length) + 1);
64 | if (format.nationalPrefix != null) {
65 | prefixPhoneFormat = "";
66 | for (i = _i = 0, _ref = format.nationalPrefix.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) {
67 | prefixPhoneFormat += ".";
68 | }
69 | phoneFormat = prefixPhoneFormat + phoneFormat;
70 | }
71 | }
72 | }
73 | if (phone.substring(0, 1) === "+") {
74 | phoneDigits = phone.substring(1);
75 | } else {
76 | phoneDigits = phone;
77 | }
78 | formatDigitCount = phoneFormat.match(/\./g).length;
79 | formattedPhone = "";
80 | for (_j = 0, _len = phoneFormat.length; _j < _len; _j++) {
81 | formatChar = phoneFormat[_j];
82 | if (formatChar === ".") {
83 | if (phoneDigits.length === 0) {
84 | break;
85 | }
86 | formattedPhone += phoneDigits.substring(0, 1);
87 | phoneDigits = phoneDigits.substring(1);
88 | } else if (lastChar || phoneDigits.length > 0) {
89 | formattedPhone += formatChar;
90 | }
91 | }
92 | phone = formattedPhone + phoneDigits;
93 | }
94 | }
95 | return phone;
96 | };
97 |
98 | isEventAllowed_ = function(e) {
99 | if (e.metaKey) {
100 | return true;
101 | }
102 | if (e.which === 32) {
103 | return false;
104 | }
105 | if (e.which === 0) {
106 | return true;
107 | }
108 | if (e.which < 33) {
109 | return true;
110 | }
111 | return isEventAllowedChar_(e);
112 | };
113 |
114 | isEventAllowedChar_ = function(e) {
115 | var char;
116 | char = String.fromCharCode(e.which);
117 | return !!/[\d\s+]/.test(char);
118 | };
119 |
120 | restrictEventAndFormat_ = function(e) {
121 | var caretEnd, value;
122 | if (!isEventAllowed_(e)) {
123 | return e.preventDefault();
124 | }
125 | if (!isEventAllowedChar_(e)) {
126 | return;
127 | }
128 | value = this.val();
129 | caretEnd = supportSelectionEnd ? this.get(0).selectionEnd : this.caret();
130 | value = value.substring(0, this.caret()) + String.fromCharCode(e.which) + value.substring(caretEnd, value.length);
131 | format_.call(this, value, e);
132 | return withTimeout((function(_this) {
133 | return function() {
134 | return _this.caret(_this.val().length);
135 | };
136 | })(this));
137 | };
138 |
139 | formatUp_ = function(e) {
140 | var value;
141 | checkForCountryChange_.call(this);
142 | value = this.val();
143 | if (e.keyCode === 8 && this.caret() === value.length) {
144 | return;
145 | }
146 | return format_.call(this, value, e);
147 | };
148 |
149 | formatBack_ = function(e) {
150 | var phone, value;
151 | if (!e) {
152 | return;
153 | }
154 | if (e.meta) {
155 | return;
156 | }
157 | value = this.val();
158 | if (value.length === 0) {
159 | return;
160 | }
161 | if (!(this.caret() === value.length)) {
162 | return;
163 | }
164 | if (e.keyCode !== 8) {
165 | return;
166 | }
167 | value = value.substring(0, value.length - 1);
168 | e.preventDefault();
169 | phone = formattedPhone_.call(this, value, false);
170 | if (this.val() !== phone) {
171 | return this.val(phone);
172 | }
173 | };
174 |
175 | format_ = function(value, e) {
176 | var phone, selection, selectionAtEnd;
177 | phone = formattedPhone_.call(this, value, true);
178 | if (phone !== this.val()) {
179 | selection = this.caret();
180 | selectionAtEnd = selection === this.val().length;
181 | e.preventDefault();
182 | this.val(phone);
183 | if (!selectionAtEnd) {
184 | return withTimeout((function(_this) {
185 | return function() {
186 | return _this.caret(selection);
187 | };
188 | })(this));
189 | }
190 | }
191 | };
192 |
193 | formattedPhone_ = function(phone, lastChar) {
194 | if (phone.indexOf('+') !== 0 && this.data('defaultPrefix')) {
195 | phone = phone.replace(/[^0-9]/g, '');
196 | } else {
197 | phone = '+' + phone.replace(/[^0-9]/g, '');
198 | }
199 | return formattedPhoneNumber_(phone, lastChar, this.data('defaultPrefix'));
200 | };
201 |
202 | checkForCountryChange_ = function() {
203 | var country, format, phone;
204 | phone = this.val();
205 | format = formatForPhone_(phone, this.data('defaultPrefix'));
206 | country = null;
207 | if (format) {
208 | country = format.country;
209 | }
210 | if (this.data('mobilePhoneCountry') !== country) {
211 | this.data('mobilePhoneCountry', country);
212 | return this.trigger('country.mobilePhoneNumber', country);
213 | }
214 | };
215 |
216 | mobilePhoneNumber = {};
217 |
218 | mobilePhoneNumber.init = function(options) {
219 | var _ref;
220 | if (options == null) {
221 | options = {};
222 | }
223 | if (!this.data('mobilePhoneNumberInited')) {
224 | this.data('mobilePhoneNumberInited', true);
225 | this.bind('keypress', (function(_this) {
226 | return function() {
227 | return restrictEventAndFormat_.apply($(_this), arguments);
228 | };
229 | })(this));
230 | this.bind('keyup', (function(_this) {
231 | return function() {
232 | return formatUp_.apply($(_this), arguments);
233 | };
234 | })(this));
235 | this.bind('keydown', (function(_this) {
236 | return function() {
237 | return formatBack_.apply($(_this), arguments);
238 | };
239 | })(this));
240 | }
241 | this.data('defaultPrefix', (_ref = options.allowPhoneWithoutPrefix) != null ? _ref : options.defaultPrefix);
242 | if (this.val() !== '') {
243 | this.val(formattedPhone_.call(this, this.val(), false));
244 | }
245 | return this;
246 | };
247 |
248 | mobilePhoneNumber.val = function() {
249 | var format, val;
250 | val = this.val().replace(/[^0-9]/g, '');
251 | format = formatForPhone_(val, this.data('defaultPrefix'));
252 | if (this.val().indexOf('+') === 0 || (this.data('defaultPrefix') == null)) {
253 | return '+' + val;
254 | } else {
255 | return this.data('defaultPrefix') + val;
256 | }
257 | };
258 |
259 | mobilePhoneNumber.validate = function() {
260 | var format, val;
261 | val = this.mobilePhoneNumber('val');
262 | format = formatForPhone_(val, this.data('defaultPrefix'));
263 | if (!format) {
264 | return true;
265 | }
266 | return val.length > format.prefix.length;
267 | };
268 |
269 | mobilePhoneNumber.country = function() {
270 | var format;
271 | format = formatForPhone_(this.mobilePhoneNumber('val'));
272 | if (format) {
273 | return format.country;
274 | }
275 | };
276 |
277 | mobilePhoneNumber.prefix = function() {
278 | var countryCode;
279 | countryCode = this.mobilePhoneNumber('country');
280 | if (countryCode == null) {
281 | return "";
282 | }
283 | return $.mobilePhoneNumberPrefixFromCountryCode(countryCode);
284 | };
285 |
286 | $.fn.mobilePhoneNumber = function() {
287 | var args, method;
288 | method = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
289 | if ((method == null) || !(typeof method === 'string')) {
290 | if (method != null) {
291 | args = [method];
292 | }
293 | method = 'init';
294 | }
295 | return mobilePhoneNumber[method].apply(this, args);
296 | };
297 |
298 | $.formatMobilePhoneNumber = function(phone) {
299 | phone = '+' + phone.replace(/[^0-9\*]/g, '');
300 | return formattedPhoneNumber_(phone, true);
301 | };
302 |
303 | $.mobilePhoneNumberPrefixFromCountryCode = function(countryCode) {
304 | var format, prefix;
305 | for (prefix in formats) {
306 | format = formats[prefix];
307 | if (format.country.toLowerCase() === countryCode.toLowerCase()) {
308 | if (prefix.length === 5 && prefix[1] === '1') {
309 | return '+1';
310 | }
311 | return prefix;
312 | }
313 | }
314 | return null;
315 | };
316 |
317 | formats = {
318 | '+247': {
319 | country: 'AC'
320 | },
321 | '+376': {
322 | country: 'AD',
323 | format: '+... ... ...'
324 | },
325 | '+971': {
326 | country: 'AE',
327 | format: '+... .. ... ....'
328 | },
329 | '+93': {
330 | country: 'AF',
331 | format: '+.. .. ... ....'
332 | },
333 | '+1268': {
334 | country: 'AG'
335 | },
336 | '+1264': {
337 | country: 'AI'
338 | },
339 | '+355': {
340 | country: 'AL',
341 | format: '+... .. ... ....'
342 | },
343 | '+374': {
344 | country: 'AM',
345 | format: '+... .. ......'
346 | },
347 | '+244': {
348 | country: 'AO',
349 | format: '+... ... ... ...'
350 | },
351 | '+54': {
352 | country: 'AR',
353 | format: '+.. .. ..-....-....'
354 | },
355 | '+1684': {
356 | country: 'AS'
357 | },
358 | '+43': {
359 | country: 'AT',
360 | format: '+.. ... ......'
361 | },
362 | '+61': {
363 | country: 'AU',
364 | format: '+.. ... ... ...'
365 | },
366 | '+297': {
367 | country: 'AW',
368 | format: '+... ... ....'
369 | },
370 | '+994': {
371 | country: 'AZ',
372 | format: '+... .. ... .. ..'
373 | },
374 | '+387': {
375 | country: 'BA',
376 | format: '+... .. ...-...'
377 | },
378 | '+1246': {
379 | country: 'BB'
380 | },
381 | '+880': {
382 | country: 'BD',
383 | format: '+... ....-......'
384 | },
385 | '+32': {
386 | country: 'BE',
387 | format: '+.. ... .. .. ..'
388 | },
389 | '+226': {
390 | country: 'BF',
391 | format: '+... .. .. .. ..'
392 | },
393 | '+359': {
394 | country: 'BG',
395 | format: '+... ... ... ..'
396 | },
397 | '+973': {
398 | country: 'BH',
399 | format: '+... .... ....'
400 | },
401 | '+257': {
402 | country: 'BI',
403 | format: '+... .. .. .. ..'
404 | },
405 | '+229': {
406 | country: 'BJ',
407 | format: '+... .. .. .. ..'
408 | },
409 | '+1441': {
410 | country: 'BM'
411 | },
412 | '+673': {
413 | country: 'BN',
414 | format: '+... ... ....'
415 | },
416 | '+591': {
417 | country: 'BO',
418 | format: '+... ........'
419 | },
420 | '+55': {
421 | country: 'BR',
422 | format: '+.. .. .....-....'
423 | },
424 | '+1242': {
425 | country: 'BS'
426 | },
427 | '+975': {
428 | country: 'BT',
429 | format: '+... .. .. .. ..'
430 | },
431 | '+267': {
432 | country: 'BW',
433 | format: '+... .. ... ...'
434 | },
435 | '+375': {
436 | country: 'BY',
437 | format: '+... .. ...-..-..'
438 | },
439 | '+501': {
440 | country: 'BZ',
441 | format: '+... ...-....'
442 | },
443 | '+243': {
444 | country: 'CD',
445 | format: '+... ... ... ...'
446 | },
447 | '+236': {
448 | country: 'CF',
449 | format: '+... .. .. .. ..'
450 | },
451 | '+242': {
452 | country: 'CG',
453 | format: '+... .. ... ....'
454 | },
455 | '+41': {
456 | country: 'CH',
457 | format: '+.. .. ... .. ..'
458 | },
459 | '+225': {
460 | country: 'CI',
461 | format: '+... .. .. .. ..'
462 | },
463 | '+682': {
464 | country: 'CK',
465 | format: '+... .. ...'
466 | },
467 | '+56': {
468 | country: 'CL',
469 | format: '+.. . .... ....'
470 | },
471 | '+237': {
472 | country: 'CM',
473 | format: '+... .. .. .. ..'
474 | },
475 | '+86': {
476 | country: 'CN',
477 | format: '+.. ... .... ....'
478 | },
479 | '+57': {
480 | country: 'CO',
481 | format: '+.. ... .......'
482 | },
483 | '+506': {
484 | country: 'CR',
485 | format: '+... .... ....'
486 | },
487 | '+53': {
488 | country: 'CU',
489 | format: '+.. . .......'
490 | },
491 | '+238': {
492 | country: 'CV',
493 | format: '+... ... .. ..'
494 | },
495 | '+599': {
496 | country: 'CW',
497 | format: '+... . ... ....'
498 | },
499 | '+537': {
500 | country: 'CY'
501 | },
502 | '+357': {
503 | country: 'CY',
504 | format: '+... .. ......'
505 | },
506 | '+420': {
507 | country: 'CZ',
508 | format: '+... ... ... ...'
509 | },
510 | '+49': {
511 | country: 'DE',
512 | format: '+.. .... .......'
513 | },
514 | '+253': {
515 | country: 'DJ',
516 | format: '+... .. .. .. ..'
517 | },
518 | '+45': {
519 | country: 'DK',
520 | format: '+.. .. .. .. ..'
521 | },
522 | '+1767': {
523 | country: 'DM'
524 | },
525 | '+1849': {
526 | country: 'DO'
527 | },
528 | '+213': {
529 | country: 'DZ',
530 | format: '+... ... .. .. ..'
531 | },
532 | '+593': {
533 | country: 'EC',
534 | format: '+... .. ... ....'
535 | },
536 | '+372': {
537 | country: 'EE',
538 | format: '+... .... ....'
539 | },
540 | '+20': {
541 | country: 'EG',
542 | format: '+.. ... ... ....'
543 | },
544 | '+291': {
545 | country: 'ER',
546 | format: '+... . ... ...'
547 | },
548 | '+34': {
549 | country: 'ES',
550 | format: '+.. ... .. .. ..'
551 | },
552 | '+251': {
553 | country: 'ET',
554 | format: '+... .. ... ....'
555 | },
556 | '+358': {
557 | country: 'FI',
558 | format: '+... .. ... .. ..'
559 | },
560 | '+679': {
561 | country: 'FJ',
562 | format: '+... ... ....'
563 | },
564 | '+500': {
565 | country: 'FK'
566 | },
567 | '+691': {
568 | country: 'FM',
569 | format: '+... ... ....'
570 | },
571 | '+298': {
572 | country: 'FO',
573 | format: '+... ......'
574 | },
575 | '+33': {
576 | country: 'FR',
577 | format: '+.. . .. .. .. ..'
578 | },
579 | '+241': {
580 | country: 'GA',
581 | format: '+... .. .. .. ..'
582 | },
583 | '+44': {
584 | country: 'GB',
585 | format: '+.. .... ......'
586 | },
587 | '+1473': {
588 | country: 'GD'
589 | },
590 | '+995': {
591 | country: 'GE',
592 | format: '+... ... .. .. ..'
593 | },
594 | '+594': {
595 | country: 'GF',
596 | format: '+... ... .. .. ..'
597 | },
598 | '+233': {
599 | country: 'GH',
600 | format: '+... .. ... ....'
601 | },
602 | '+350': {
603 | country: 'GI',
604 | format: '+... ... .....'
605 | },
606 | '+299': {
607 | country: 'GL',
608 | format: '+... .. .. ..'
609 | },
610 | '+220': {
611 | country: 'GM',
612 | format: '+... ... ....'
613 | },
614 | '+224': {
615 | country: 'GN',
616 | format: '+... ... .. .. ..'
617 | },
618 | '+240': {
619 | country: 'GQ',
620 | format: '+... ... ... ...'
621 | },
622 | '+30': {
623 | country: 'GR',
624 | format: '+.. ... ... ....'
625 | },
626 | '+502': {
627 | country: 'GT',
628 | format: '+... .... ....'
629 | },
630 | '+1671': {
631 | country: 'GU'
632 | },
633 | '+245': {
634 | country: 'GW',
635 | format: '+... ... ....'
636 | },
637 | '+592': {
638 | country: 'GY',
639 | format: '+... ... ....'
640 | },
641 | '+852': {
642 | country: 'HK',
643 | format: '+... .... ....'
644 | },
645 | '+504': {
646 | country: 'HN',
647 | format: '+... ....-....'
648 | },
649 | '+385': {
650 | country: 'HR',
651 | format: '+... .. ... ....'
652 | },
653 | '+509': {
654 | country: 'HT',
655 | format: '+... .. .. ....'
656 | },
657 | '+36': {
658 | country: 'HU',
659 | format: '+.. .. ... ....'
660 | },
661 | '+62': {
662 | country: 'ID',
663 | format: '+.. ...-...-...'
664 | },
665 | '+353': {
666 | country: 'IE',
667 | format: '+... .. ... ....'
668 | },
669 | '+972': {
670 | country: 'IL',
671 | format: '+... ..-...-....'
672 | },
673 | '+91': {
674 | country: 'IN',
675 | format: '+.. .. .. ......'
676 | },
677 | '+246': {
678 | country: 'IO',
679 | format: '+... ... ....'
680 | },
681 | '+964': {
682 | country: 'IQ',
683 | format: '+... ... ... ....'
684 | },
685 | '+98': {
686 | country: 'IR',
687 | format: '+.. ... ... ....'
688 | },
689 | '+354': {
690 | country: 'IS',
691 | format: '+... ... ....'
692 | },
693 | '+39': {
694 | country: 'IT',
695 | format: '+.. .. .... ....'
696 | },
697 | '+1876': {
698 | country: 'JM'
699 | },
700 | '+962': {
701 | country: 'JO',
702 | format: '+... . .... ....'
703 | },
704 | '+81': {
705 | country: 'JP',
706 | format: '+.. ..-....-....',
707 | nationalPrefix: '0'
708 | },
709 | '+254': {
710 | country: 'KE',
711 | format: '+... .. .......'
712 | },
713 | '+996': {
714 | country: 'KG',
715 | format: '+... ... ... ...'
716 | },
717 | '+855': {
718 | country: 'KH',
719 | format: '+... .. ... ...'
720 | },
721 | '+686': {
722 | country: 'KI'
723 | },
724 | '+269': {
725 | country: 'KM',
726 | format: '+... ... .. ..'
727 | },
728 | '+1869': {
729 | country: 'KN'
730 | },
731 | '+850': {
732 | country: 'KP',
733 | format: '+... ... ... ....'
734 | },
735 | '+82': {
736 | country: 'KR',
737 | format: '+.. ..-....-....'
738 | },
739 | '+965': {
740 | country: 'KW',
741 | format: '+... ... .....'
742 | },
743 | '+345': {
744 | country: 'KY'
745 | },
746 | '+77': {
747 | country: 'KZ'
748 | },
749 | '+856': {
750 | country: 'LA',
751 | format: '+... .. .. ... ...'
752 | },
753 | '+961': {
754 | country: 'LB',
755 | format: '+... .. ... ...'
756 | },
757 | '+1758': {
758 | country: 'LC'
759 | },
760 | '+423': {
761 | country: 'LI',
762 | format: '+... ... ... ...'
763 | },
764 | '+94': {
765 | country: 'LK',
766 | format: '+.. .. . ......'
767 | },
768 | '+231': {
769 | country: 'LR',
770 | format: '+... ... ... ...'
771 | },
772 | '+266': {
773 | country: 'LS',
774 | format: '+... .... ....'
775 | },
776 | '+370': {
777 | country: 'LT',
778 | format: '+... ... .....'
779 | },
780 | '+352': {
781 | country: 'LU',
782 | format: '+... .. .. .. ...'
783 | },
784 | '+371': {
785 | country: 'LV',
786 | format: '+... .. ... ...'
787 | },
788 | '+218': {
789 | country: 'LY',
790 | format: '+... ..-.......'
791 | },
792 | '+212': {
793 | country: 'MA',
794 | format: '+... ...-......'
795 | },
796 | '+377': {
797 | country: 'MC',
798 | format: '+... . .. .. .. ..'
799 | },
800 | '+373': {
801 | country: 'MD',
802 | format: '+... ... .. ...'
803 | },
804 | '+382': {
805 | country: 'ME',
806 | format: '+... .. ... ...'
807 | },
808 | '+590': {
809 | country: 'MF'
810 | },
811 | '+261': {
812 | country: 'MG',
813 | format: '+... .. .. ... ..'
814 | },
815 | '+692': {
816 | country: 'MH',
817 | format: '+... ...-....'
818 | },
819 | '+389': {
820 | country: 'MK',
821 | format: '+... .. ... ...'
822 | },
823 | '+223': {
824 | country: 'ML',
825 | format: '+... .. .. .. ..'
826 | },
827 | '+95': {
828 | country: 'MM',
829 | format: '+.. . ... ....'
830 | },
831 | '+976': {
832 | country: 'MN',
833 | format: '+... .... ....'
834 | },
835 | '+853': {
836 | country: 'MO',
837 | format: '+... .... ....'
838 | },
839 | '+1670': {
840 | country: 'MP'
841 | },
842 | '+596': {
843 | country: 'MQ',
844 | format: '+... ... .. .. ..'
845 | },
846 | '+222': {
847 | country: 'MR',
848 | format: '+... .. .. .. ..'
849 | },
850 | '+1664': {
851 | country: 'MS'
852 | },
853 | '+356': {
854 | country: 'MT',
855 | format: '+... .... ....'
856 | },
857 | '+230': {
858 | country: 'MU',
859 | format: '+... .... ....'
860 | },
861 | '+960': {
862 | country: 'MV',
863 | format: '+... ...-....'
864 | },
865 | '+265': {
866 | country: 'MW',
867 | format: '+... ... .. .. ..'
868 | },
869 | '+52': {
870 | country: 'MX',
871 | format: '+.. ... ... ... ....'
872 | },
873 | '+60': {
874 | country: 'MY',
875 | format: '+.. ..-... ....'
876 | },
877 | '+258': {
878 | country: 'MZ',
879 | format: '+... .. ... ....'
880 | },
881 | '+264': {
882 | country: 'NA',
883 | format: '+... .. ... ....'
884 | },
885 | '+687': {
886 | country: 'NC',
887 | format: '+... ........'
888 | },
889 | '+227': {
890 | country: 'NE',
891 | format: '+... .. .. .. ..'
892 | },
893 | '+672': {
894 | country: 'NF',
895 | format: '+... .. ....'
896 | },
897 | '+234': {
898 | country: 'NG',
899 | format: '+... ... ... ....'
900 | },
901 | '+505': {
902 | country: 'NI',
903 | format: '+... .... ....'
904 | },
905 | '+31': {
906 | country: 'NL',
907 | format: '+.. . ........'
908 | },
909 | '+47': {
910 | country: 'NO',
911 | format: '+.. ... .. ...'
912 | },
913 | '+977': {
914 | country: 'NP',
915 | format: '+... ...-.......'
916 | },
917 | '+674': {
918 | country: 'NR',
919 | format: '+... ... ....'
920 | },
921 | '+683': {
922 | country: 'NU'
923 | },
924 | '+64': {
925 | country: 'NZ',
926 | format: '+.. .. ... ....'
927 | },
928 | '+968': {
929 | country: 'OM',
930 | format: '+... .... ....'
931 | },
932 | '+507': {
933 | country: 'PA',
934 | format: '+... ....-....'
935 | },
936 | '+51': {
937 | country: 'PE',
938 | format: '+.. ... ... ...'
939 | },
940 | '+689': {
941 | country: 'PF',
942 | format: '+... .. .. ..'
943 | },
944 | '+675': {
945 | country: 'PG',
946 | format: '+... ... ....'
947 | },
948 | '+63': {
949 | country: 'PH',
950 | format: '+.. .... ......'
951 | },
952 | '+92': {
953 | country: 'PK',
954 | format: '+.. ... .......'
955 | },
956 | '+48': {
957 | country: 'PL',
958 | format: '+.. .. ... .. ..'
959 | },
960 | '+508': {
961 | country: 'PM',
962 | format: '+... .. .. ..'
963 | },
964 | '+872': {
965 | country: 'PN'
966 | },
967 | '+1939': {
968 | country: 'PR'
969 | },
970 | '+970': {
971 | country: 'PS',
972 | format: '+... ... ... ...'
973 | },
974 | '+351': {
975 | country: 'PT',
976 | format: '+... ... ... ...'
977 | },
978 | '+680': {
979 | country: 'PW',
980 | format: '+... ... ....'
981 | },
982 | '+595': {
983 | country: 'PY',
984 | format: '+... .. .......'
985 | },
986 | '+974': {
987 | country: 'QA',
988 | format: '+... .... ....'
989 | },
990 | '+262': {
991 | country: 'RE'
992 | },
993 | '+40': {
994 | country: 'RO',
995 | format: '+.. .. ... ....'
996 | },
997 | '+381': {
998 | country: 'RS',
999 | format: '+... .. .......'
1000 | },
1001 | '+7': {
1002 | country: 'RU',
1003 | format: '+. ... ...-..-..'
1004 | },
1005 | '+250': {
1006 | country: 'RW',
1007 | format: '+... ... ... ...'
1008 | },
1009 | '+966': {
1010 | country: 'SA',
1011 | format: '+... .. ... ....'
1012 | },
1013 | '+677': {
1014 | country: 'SB',
1015 | format: '+... ... ....'
1016 | },
1017 | '+248': {
1018 | country: 'SC',
1019 | format: '+... . ... ...'
1020 | },
1021 | '+249': {
1022 | country: 'SD',
1023 | format: '+... .. ... ....'
1024 | },
1025 | '+46': {
1026 | country: 'SE',
1027 | format: '+.. ..-... .. ..'
1028 | },
1029 | '+65': {
1030 | country: 'SG',
1031 | format: '+.. .... ....'
1032 | },
1033 | '+290': {
1034 | country: 'SH'
1035 | },
1036 | '+386': {
1037 | country: 'SI',
1038 | format: '+... .. ... ...'
1039 | },
1040 | '+421': {
1041 | country: 'SK',
1042 | format: '+... ... ... ...'
1043 | },
1044 | '+232': {
1045 | country: 'SL',
1046 | format: '+... .. ......'
1047 | },
1048 | '+378': {
1049 | country: 'SM',
1050 | format: '+... .. .. .. ..'
1051 | },
1052 | '+221': {
1053 | country: 'SN',
1054 | format: '+... .. ... .. ..'
1055 | },
1056 | '+252': {
1057 | country: 'SO',
1058 | format: '+... .. .......'
1059 | },
1060 | '+597': {
1061 | country: 'SR',
1062 | format: '+... ...-....'
1063 | },
1064 | '+211': {
1065 | country: 'SS',
1066 | format: '+... ... ... ...'
1067 | },
1068 | '+239': {
1069 | country: 'ST',
1070 | format: '+... ... ....'
1071 | },
1072 | '+503': {
1073 | country: 'SV',
1074 | format: '+... .... ....'
1075 | },
1076 | '+963': {
1077 | country: 'SY',
1078 | format: '+... ... ... ...'
1079 | },
1080 | '+268': {
1081 | country: 'SZ',
1082 | format: '+... .... ....'
1083 | },
1084 | '+1649': {
1085 | country: 'TC'
1086 | },
1087 | '+235': {
1088 | country: 'TD',
1089 | format: '+... .. .. .. ..'
1090 | },
1091 | '+228': {
1092 | country: 'TG',
1093 | format: '+... .. .. .. ..'
1094 | },
1095 | '+66': {
1096 | country: 'TH',
1097 | format: '+.. .. ... ....'
1098 | },
1099 | '+992': {
1100 | country: 'TJ',
1101 | format: '+... ... .. ....'
1102 | },
1103 | '+690': {
1104 | country: 'TK'
1105 | },
1106 | '+670': {
1107 | country: 'TL',
1108 | format: '+... .... ....'
1109 | },
1110 | '+993': {
1111 | country: 'TM',
1112 | format: '+... .. ..-..-..'
1113 | },
1114 | '+216': {
1115 | country: 'TN',
1116 | format: '+... .. ... ...'
1117 | },
1118 | '+676': {
1119 | country: 'TO',
1120 | format: '+... ... ....'
1121 | },
1122 | '+90': {
1123 | country: 'TR',
1124 | format: '+.. ... ... ....'
1125 | },
1126 | '+1868': {
1127 | country: 'TT'
1128 | },
1129 | '+688': {
1130 | country: 'TV'
1131 | },
1132 | '+886': {
1133 | country: 'TW',
1134 | format: '+... ... ... ...'
1135 | },
1136 | '+255': {
1137 | country: 'TZ',
1138 | format: '+... ... ... ...'
1139 | },
1140 | '+380': {
1141 | country: 'UA',
1142 | format: '+... .. ... ....'
1143 | },
1144 | '+256': {
1145 | country: 'UG',
1146 | format: '+... ... ......'
1147 | },
1148 | '+1': {
1149 | country: 'US'
1150 | },
1151 | '+598': {
1152 | country: 'UY',
1153 | format: '+... .... ....'
1154 | },
1155 | '+998': {
1156 | country: 'UZ',
1157 | format: '+... .. ... .. ..'
1158 | },
1159 | '+379': {
1160 | country: 'VA'
1161 | },
1162 | '+1784': {
1163 | country: 'VC'
1164 | },
1165 | '+58': {
1166 | country: 'VE',
1167 | format: '+.. ...-.......'
1168 | },
1169 | '+1284': {
1170 | country: 'VG'
1171 | },
1172 | '+1340': {
1173 | country: 'VI'
1174 | },
1175 | '+84': {
1176 | country: 'VN',
1177 | format: '+.. .. ... .. ..'
1178 | },
1179 | '+678': {
1180 | country: 'VU',
1181 | format: '+... ... ....'
1182 | },
1183 | '+681': {
1184 | country: 'WF',
1185 | format: '+... .. .. ..'
1186 | },
1187 | '+685': {
1188 | country: 'WS'
1189 | },
1190 | '+967': {
1191 | country: 'YE',
1192 | format: '+... ... ... ...'
1193 | },
1194 | '+27': {
1195 | country: 'ZA',
1196 | format: '+.. .. ... ....'
1197 | },
1198 | '+260': {
1199 | country: 'ZM',
1200 | format: '+... .. .......'
1201 | },
1202 | '+263': {
1203 | country: 'ZW',
1204 | format: '+... .. ... ....'
1205 | }
1206 | };
1207 |
1208 | (function(formats) {
1209 | var canadaPrefixes, format, prefix, _i, _len, _results;
1210 | canadaPrefixes = [403, 587, 780, 250, 604, 778, 204, 506, 709, 902, 226, 249, 289, 343, 416, 519, 613, 647, 705, 807, 905, 418, 438, 450, 514, 579, 581, 819, 873, 306, 867];
1211 | for (_i = 0, _len = canadaPrefixes.length; _i < _len; _i++) {
1212 | prefix = canadaPrefixes[_i];
1213 | formats['+1' + prefix] = {
1214 | country: 'CA'
1215 | };
1216 | }
1217 | _results = [];
1218 | for (prefix in formats) {
1219 | format = formats[prefix];
1220 | if (prefix.substring(0, 2) === "+1") {
1221 | _results.push(format.format = '+. (...) ...-....');
1222 | } else {
1223 | _results.push(void 0);
1224 | }
1225 | }
1226 | return _results;
1227 | })(formats);
1228 |
1229 | }).call(this);
1230 |
--------------------------------------------------------------------------------