Skip to content

Commit a788bd5

Browse files
committed
handle full width input modes
stripe-archive/jquery.payment#172
1 parent 982ba66 commit a788bd5

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

src/payform.coffee

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,22 @@
171171

172172
# Private
173173

174+
# Replace Full-Width Chars
175+
176+
replaceFullWidthChars = (str = '') ->
177+
fullWidth = '\uff10\uff11\uff12\uff13\uff14\uff15\uff16\uff17\uff18\uff19'
178+
halfWidth = '0123456789'
179+
180+
value = ''
181+
chars = str.split('')
182+
183+
for char in chars
184+
idx = fullWidth.indexOf(char)
185+
char = halfWidth[idx] if idx > -1
186+
value += char
187+
188+
value
189+
174190
# Format Card Number
175191

176192
reFormatCardNumber = (e) ->
@@ -287,7 +303,7 @@
287303

288304
reFormatCVC = (e) ->
289305
cursor = _getCaretPos(e.target)
290-
e.target.value = e.target.value.replace(/\D/g, '')[0...4]
306+
e.target.value = replaceFullWidthChars(e.target.value).replace(/\D/g, '')[0...4]
291307
if cursor? and e.type isnt 'change'
292308
e.target.setSelectionRange(cursor, cursor)
293309

@@ -459,6 +475,7 @@
459475
cardFromNumber(num)?.type or null
460476

461477
payform.formatCardNumber = (num) ->
478+
num = replaceFullWidthChars(num)
462479
num = num.replace(/\D/g, '')
463480
card = cardFromNumber(num)
464481
return num unless card
@@ -476,6 +493,7 @@
476493
groups.join(' ')
477494

478495
payform.formatCardExpiry = (expiry) ->
496+
expiry = replaceFullWidthChars(expiry)
479497
parts = expiry.match(/^\D*(\d{1,2})(\D+)?(\d{1,4})?/)
480498
return '' unless parts
481499

test/formatCardExpiry_spec.coffee

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@ describe 'payform', ->
88

99
it 'should only allow numbers', ->
1010
assert payform.formatCardExpiry('1d'), '01 / '
11+
12+
it 'should format full-width expiry correctly', ->
13+
assert payform.formatCardExpiry('\uff10\uff18'), '08 /'
14+
assert payform.formatCardNumber('\uff10\uff18\uff11\uff15'), '08 / 15'

test/formatCardNumber_spec.coffee

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,10 @@ describe 'payform', ->
88
assert payform.formatCardNumber('42424242'), '4242 4242'
99
assert payform.formatCardNumber('4242424242'), '4242 4242 42'
1010
assert payform.formatCardNumber('4242424242424242'), '4242 4242 4242 4242'
11+
1112
it 'should format amex cc number correctly', ->
1213
assert payform.formatCardNumber('37828'), '3782 8'
14+
15+
it 'should format full-width cc number correctly', ->
16+
assert payform.formatCardNumber('\uff14\uff12\uff14\uff12'), '4242'
17+
assert payform.formatCardNumber('\uff14\uff12\uff14\uff12\uff14\uff12'), '4242 42'

0 commit comments

Comments
 (0)