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

Commit

Permalink
Support for nationalPrefix
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelvillar committed Aug 25, 2014
1 parent f3206f4 commit 79a2235
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/jquery.mobilePhoneNumber.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ formatForPhone_ = (phone, defaultPrefix = null) ->
precision = 0
for prefix, format of formats
if phone.length >= prefix.length && phone.substring(0, prefix.length) == prefix && prefix.length > precision
bestFormat = format
bestFormat = {}
for k, v of format
bestFormat[k] = v
bestFormat.prefix = prefix
precision = prefix.length
bestFormat
Expand All @@ -30,6 +32,11 @@ formattedPhoneNumber_ = (phone, lastChar, defaultPrefix = null) ->
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 format.nationalPrefix?
prefixPhoneFormat = ""
for i in [0...format.nationalPrefix.length]
prefixPhoneFormat += "."
phoneFormat = prefixPhoneFormat + phoneFormat

if phone.substring(0, 1) == "+"
phoneDigits = phone.substring(1)
Expand Down
6 changes: 6 additions & 0 deletions test/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ describe 'jquery.mobilePhoneNumber', ->
type $phone, '1234567'
assert.equal $phone.val(), '(415) 123-4567'

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

type $phone, '08043691337'
assert.equal $phone.val(), '0804-369-1337'

it 'should correctly format BE phone', ->
$phone = $('<input type=text>').val('').mobilePhoneNumber()

Expand Down

0 comments on commit 79a2235

Please sign in to comment.