Skip to content
This repository has been archived by the owner on Apr 8, 2020. It is now read-only.

Commit

Permalink
Renamed allowPhoneWithoutPrefix property to defaultPrefix
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelvillar committed Aug 25, 2014
1 parent 364b84d commit 69befcd
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 26 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ Enables phone number formatting.

Options:

* `allowPhoneWithoutPrefix`: allows the user to type a phone number without the prefix for this specific value.
* `defaultPrefix`: allows the user to type a phone number without the prefix for this specific value.

Example:

``` javascript
$('input.phone-num').mobilePhoneNumber({
allowPhoneWithoutPrefix: '+1'
defaultPrefix: '+1'
});
```

Expand Down
35 changes: 18 additions & 17 deletions src/jquery.mobilePhoneNumber.coffee
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
$ = jQuery

formatForPhone_ = (phone, allowPhoneWithoutPrefix = null) ->
if phone.indexOf('+') != 0 and allowPhoneWithoutPrefix
phone = allowPhoneWithoutPrefix + phone.replace(/[^0-9]/g, '')
formatForPhone_ = (phone, defaultPrefix = null) ->
if phone.indexOf('+') != 0 and defaultPrefix
phone = defaultPrefix + phone.replace(/[^0-9]/g, '')
else
phone = '+' + phone.replace(/[^0-9]/g, '')
bestFormat = null
Expand All @@ -20,16 +20,16 @@ prefixesAreSubsets_ = (prefixA, prefixB) ->
return prefixB.substring(0, prefixA.length) == prefixA
return prefixA.substring(0, prefixB.length) == prefixB

formattedPhoneNumber_ = (phone, lastChar, allowPhoneWithoutPrefix = null) ->
if phone.length != 0 and (phone.substring(0, 1) == "+" or allowPhoneWithoutPrefix)
format = formatForPhone_(phone, allowPhoneWithoutPrefix)
formattedPhoneNumber_ = (phone, lastChar, defaultPrefix = null) ->
if phone.length != 0 and (phone.substring(0, 1) == "+" or defaultPrefix)
format = formatForPhone_(phone, defaultPrefix)
if format && format.format
phoneFormat = format.format
phonePrefix = format.prefix

if allowPhoneWithoutPrefix
if (allowPhoneWithoutPrefix == phonePrefix or prefixesAreSubsets_(phonePrefix, allowPhoneWithoutPrefix)) and (phone.indexOf('+') != 0 or phone.length == 0)
phoneFormat = phoneFormat.substring(Math.min(phonePrefix.length, allowPhoneWithoutPrefix.length) + 1)
if defaultPrefix
if (defaultPrefix == phonePrefix or prefixesAreSubsets_(phonePrefix, defaultPrefix)) and (phone.indexOf('+') != 0 or phone.length == 0)
phoneFormat = phoneFormat.substring(Math.min(phonePrefix.length, defaultPrefix.length) + 1)

if phone.substring(0, 1) == "+"
phoneDigits = phone.substring(1)
Expand Down Expand Up @@ -115,15 +115,15 @@ format_ = (value, e) ->
@[0].selectionEnd = selection[1]

formattedPhone_ = (phone, lastChar) ->
if phone.indexOf('+') != 0 && @data('allowPhoneWithoutPrefix')
if phone.indexOf('+') != 0 && @data('defaultPrefix')
phone = phone.replace(/[^0-9]/g, '')
else
phone = '+' + phone.replace(/[^0-9]/g, '')
formattedPhoneNumber_(phone, lastChar, @data('allowPhoneWithoutPrefix'))
formattedPhoneNumber_(phone, lastChar, @data('defaultPrefix'))

checkForCountryChange_ = ->
phone = @val()
format = formatForPhone_(phone, @data('allowPhoneWithoutPrefix'))
format = formatForPhone_(phone, @data('defaultPrefix'))
country = null
country = format.country if format
if @data('mobilePhoneCountry') != country
Expand All @@ -141,19 +141,19 @@ mobilePhoneNumber.init = (options = {}) ->
@bind 'keydown', =>
formatBack_.apply($(@), arguments)

@data('allowPhoneWithoutPrefix', options.allowPhoneWithoutPrefix)
@data('defaultPrefix', options.allowPhoneWithoutPrefix ? options.defaultPrefix)

mobilePhoneNumber.val = ->
val = @val().replace(/[^0-9]/g, '')
format = formatForPhone_(val, @data('allowPhoneWithoutPrefix'))
if @val().indexOf('+') == 0 or !@data('allowPhoneWithoutPrefix')?
format = formatForPhone_(val, @data('defaultPrefix'))
if @val().indexOf('+') == 0 or !@data('defaultPrefix')?
'+' + val
else
@data('allowPhoneWithoutPrefix') + val
@data('defaultPrefix') + val

mobilePhoneNumber.validate = ->
val = @mobilePhoneNumber('val')
format = formatForPhone_(val, @data('allowPhoneWithoutPrefix'))
format = formatForPhone_(val, @data('defaultPrefix'))
return true unless format
return val.length > format.prefix.length

Expand Down Expand Up @@ -474,6 +474,7 @@ formats =
'+81' :
country : 'JP',
format : '+.. ...-...-....',
nationalPrefix: '0',

This comment has been minimized.

Copy link
@jamesreggio

jamesreggio Aug 25, 2014

Contributor

What is this used for?

This comment has been minimized.

Copy link
@jamesreggio

jamesreggio Aug 25, 2014

Contributor

Nevermind; didn't see 79a2235.

'+254' :
country : 'KE',
format : '+... .. .......',
Expand Down
14 changes: 7 additions & 7 deletions test/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ describe 'jquery.mobilePhoneNumber', ->
type $phone, '4567'
assert.equal $phone.val(), '+1 (415) 123-4567'

it 'should correctly format US phone with allowPhoneWithoutPrefix +1', ->
$phone = $('<input type=text>').val('').mobilePhoneNumber({ allowPhoneWithoutPrefix: '+1' })
it 'should correctly format US phone with defaultPrefix +1', ->
$phone = $('<input type=text>').val('').mobilePhoneNumber({ defaultPrefix: '+1' })

type $phone, '415'
assert.equal $phone.val(), '(415) '
Expand Down Expand Up @@ -79,8 +79,8 @@ describe 'jquery.mobilePhoneNumber', ->
type $phone, '3456'
assert.equal $phone.val(), '+32 495 12 34 56'

it 'should correctly format BE phone with allowPhoneWithoutPrefix +1', ->
$phone = $('<input type=text>').val('').mobilePhoneNumber({ allowPhoneWithoutPrefix: '+1' })
it 'should correctly format BE phone with defaultPrefix +1', ->
$phone = $('<input type=text>').val('').mobilePhoneNumber({ defaultPrefix: '+1' })

type $phone, '+32'
assert.equal $phone.val(), '+32 '
Expand Down Expand Up @@ -121,16 +121,16 @@ describe 'jquery.mobilePhoneNumber', ->
assert.equal $phone.mobilePhoneNumber('prefix'), '+1'

describe 'mobilePhoneNumber("val")', ->
it 'should correctly returns the val with allowPhoneWithoutPrefix on', ->
$phone = $('<input type=text>').mobilePhoneNumber({allowPhoneWithoutPrefix: '+1'})
it 'should correctly returns the val with defaultPrefix on', ->
$phone = $('<input type=text>').mobilePhoneNumber({defaultPrefix: '+1'})

$phone.val('4151234567')
assert.equal $phone.mobilePhoneNumber('val'), '+14151234567'

$phone.val('+32123456789')
assert.equal $phone.mobilePhoneNumber('val'), '+32123456789'

it 'should correctly returns the val with allowPhoneWithoutPrefix off', ->
it 'should correctly returns the val with defaultPrefix off', ->
$phone = $('<input type=text>').mobilePhoneNumber()

$phone.val('+14151234567')
Expand Down

0 comments on commit 69befcd

Please sign in to comment.