File tree Expand file tree Collapse file tree 3 files changed +28
-1
lines changed Expand file tree Collapse file tree 3 files changed +28
-1
lines changed Original file line number Diff line number Diff line change 171
171
172
172
# Private
173
173
174
+ # Replace Full-Width Chars
175
+
176
+ replaceFullWidthChars = (str = ' ' ) ->
177
+ fullWidth = ' \u ff10\u ff11\u ff12\u ff13\u ff14\u ff15\u ff16\u ff17\u ff18\u ff19'
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
+
174
190
# Format Card Number
175
191
176
192
reFormatCardNumber = (e ) ->
287
303
288
304
reFormatCVC = (e ) ->
289
305
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 ]
291
307
if cursor? and e .type isnt ' change'
292
308
e .target .setSelectionRange (cursor, cursor)
293
309
459
475
cardFromNumber (num)? .type or null
460
476
461
477
payform .formatCardNumber = (num ) ->
478
+ num = replaceFullWidthChars (num)
462
479
num = num .replace (/ \D / g , ' ' )
463
480
card = cardFromNumber (num)
464
481
return num unless card
476
493
groups .join (' ' )
477
494
478
495
payform .formatCardExpiry = (expiry ) ->
496
+ expiry = replaceFullWidthChars (expiry)
479
497
parts = expiry .match (/ ^ \D * (\d {1,2} )(\D + )? (\d {1,4} )? / )
480
498
return ' ' unless parts
481
499
Original file line number Diff line number Diff line change @@ -8,3 +8,7 @@ describe 'payform', ->
8
8
9
9
it ' should only allow numbers' , ->
10
10
assert payform .formatCardExpiry (' 1d' ), ' 01 / '
11
+
12
+ it ' should format full-width expiry correctly' , ->
13
+ assert payform .formatCardExpiry (' \u ff10\u ff18' ), ' 08 /'
14
+ assert payform .formatCardNumber (' \u ff10\u ff18\u ff11\u ff15' ), ' 08 / 15'
Original file line number Diff line number Diff line change @@ -8,5 +8,10 @@ describe 'payform', ->
8
8
assert payform .formatCardNumber (' 42424242' ), ' 4242 4242'
9
9
assert payform .formatCardNumber (' 4242424242' ), ' 4242 4242 42'
10
10
assert payform .formatCardNumber (' 4242424242424242' ), ' 4242 4242 4242 4242'
11
+
11
12
it ' should format amex cc number correctly' , ->
12
13
assert payform .formatCardNumber (' 37828' ), ' 3782 8'
14
+
15
+ it ' should format full-width cc number correctly' , ->
16
+ assert payform .formatCardNumber (' \u ff14\u ff12\u ff14\u ff12' ), ' 4242'
17
+ assert payform .formatCardNumber (' \u ff14\u ff12\u ff14\u ff12\u ff14\u ff12' ), ' 4242 42'
You can’t perform that action at this time.
0 commit comments