|
9 | 9 | */ |
10 | 10 |
|
11 | 11 | (function() { |
12 | | - var __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; |
| 12 | + var indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; |
13 | 13 |
|
14 | 14 | (function(name, definition) { |
15 | 15 | if (typeof module !== "undefined" && module !== null) { |
|
20 | 20 | return this[name] = definition(); |
21 | 21 | } |
22 | 22 | })('payform', function() { |
23 | | - var cardFromNumber, cardFromType, defaultFormat, formatBackCardNumber, formatBackExpiry, formatCardExpiry, formatCardNumber, formatForwardExpiry, formatForwardSlashAndSpace, hasTextSelected, luhnCheck, payform, reFormatCVC, reFormatCardNumber, reFormatExpiry, restrictCVC, restrictCardNumber, restrictExpiry, restrictNumeric, _eventNormalize, _getCaretPos, _on; |
| 23 | + var _eventNormalize, _getCaretPos, _on, cardFromNumber, cardFromType, defaultFormat, formatBackCardNumber, formatBackExpiry, formatCardExpiry, formatCardNumber, formatForwardExpiry, formatForwardSlashAndSpace, hasTextSelected, luhnCheck, payform, reFormatCVC, reFormatCardNumber, reFormatExpiry, replaceFullWidthChars, restrictCVC, restrictCardNumber, restrictExpiry, restrictNumeric; |
24 | 24 | _getCaretPos = function(ele) { |
25 | 25 | var r, rc, re; |
26 | 26 | if (ele.selectionStart != null) { |
|
98 | 98 | luhn: true |
99 | 99 | }, { |
100 | 100 | type: 'mastercard', |
101 | | - pattern: /^5[0-5]/, |
| 101 | + pattern: /^(5[1-5]|2[2-7])/, |
102 | 102 | format: defaultFormat, |
103 | 103 | length: [16], |
104 | 104 | cvcLength: [3], |
|
113 | 113 | }, { |
114 | 114 | type: 'dinersclub', |
115 | 115 | pattern: /^3[0689]/, |
116 | | - format: defaultFormat, |
| 116 | + format: /(\d{1,4})(\d{1,6})?(\d{1,4})?/, |
117 | 117 | length: [14], |
118 | 118 | cvcLength: [3], |
119 | 119 | luhn: true |
|
141 | 141 | } |
142 | 142 | ]; |
143 | 143 | cardFromNumber = function(num) { |
144 | | - var card, _i, _len, _ref; |
| 144 | + var card, i, len, ref; |
145 | 145 | num = (num + '').replace(/\D/g, ''); |
146 | | - _ref = payform.cards; |
147 | | - for (_i = 0, _len = _ref.length; _i < _len; _i++) { |
148 | | - card = _ref[_i]; |
| 146 | + ref = payform.cards; |
| 147 | + for (i = 0, len = ref.length; i < len; i++) { |
| 148 | + card = ref[i]; |
149 | 149 | if (card.pattern.test(num)) { |
150 | 150 | return card; |
151 | 151 | } |
152 | 152 | } |
153 | 153 | }; |
154 | 154 | cardFromType = function(type) { |
155 | | - var card, _i, _len, _ref; |
156 | | - _ref = payform.cards; |
157 | | - for (_i = 0, _len = _ref.length; _i < _len; _i++) { |
158 | | - card = _ref[_i]; |
| 155 | + var card, i, len, ref; |
| 156 | + ref = payform.cards; |
| 157 | + for (i = 0, len = ref.length; i < len; i++) { |
| 158 | + card = ref[i]; |
159 | 159 | if (card.type === type) { |
160 | 160 | return card; |
161 | 161 | } |
162 | 162 | } |
163 | 163 | }; |
164 | 164 | luhnCheck = function(num) { |
165 | | - var digit, digits, odd, sum, _i, _len; |
| 165 | + var digit, digits, i, len, odd, sum; |
166 | 166 | odd = true; |
167 | 167 | sum = 0; |
168 | 168 | digits = (num + '').split('').reverse(); |
169 | | - for (_i = 0, _len = digits.length; _i < _len; _i++) { |
170 | | - digit = digits[_i]; |
| 169 | + for (i = 0, len = digits.length; i < len; i++) { |
| 170 | + digit = digits[i]; |
171 | 171 | digit = parseInt(digit, 10); |
172 | 172 | if ((odd = !odd)) { |
173 | 173 | digit *= 2; |
|
180 | 180 | return sum % 10 === 0; |
181 | 181 | }; |
182 | 182 | hasTextSelected = function(target) { |
183 | | - var _ref; |
184 | | - if ((typeof document !== "undefined" && document !== null ? (_ref = document.selection) != null ? _ref.createRange : void 0 : void 0) != null) { |
| 183 | + var ref; |
| 184 | + if ((typeof document !== "undefined" && document !== null ? (ref = document.selection) != null ? ref.createRange : void 0 : void 0) != null) { |
185 | 185 | if (document.selection.createRange().text) { |
186 | 186 | return true; |
187 | 187 | } |
188 | 188 | } |
189 | 189 | return (target.selectionStart != null) && target.selectionStart !== target.selectionEnd; |
190 | 190 | }; |
| 191 | + replaceFullWidthChars = function(str) { |
| 192 | + var char, chars, fullWidth, halfWidth, i, idx, len, value; |
| 193 | + if (str == null) { |
| 194 | + str = ''; |
| 195 | + } |
| 196 | + fullWidth = '\uff10\uff11\uff12\uff13\uff14\uff15\uff16\uff17\uff18\uff19'; |
| 197 | + halfWidth = '0123456789'; |
| 198 | + value = ''; |
| 199 | + chars = str.split(''); |
| 200 | + for (i = 0, len = chars.length; i < len; i++) { |
| 201 | + char = chars[i]; |
| 202 | + idx = fullWidth.indexOf(char); |
| 203 | + if (idx > -1) { |
| 204 | + char = halfWidth[idx]; |
| 205 | + } |
| 206 | + value += char; |
| 207 | + } |
| 208 | + return value; |
| 209 | + }; |
191 | 210 | reFormatCardNumber = function(e) { |
192 | 211 | var cursor; |
193 | 212 | cursor = _getCaretPos(e.target); |
|
324 | 343 | reFormatCVC = function(e) { |
325 | 344 | var cursor; |
326 | 345 | cursor = _getCaretPos(e.target); |
327 | | - e.target.value = e.target.value.replace(/\D/g, '').slice(0, 4); |
| 346 | + e.target.value = replaceFullWidthChars(e.target.value).replace(/\D/g, '').slice(0, 4); |
328 | 347 | if ((cursor != null) && e.type !== 'change') { |
329 | 348 | return e.target.setSelectionRange(cursor, cursor); |
330 | 349 | } |
|
424 | 443 | return _on(input, 'input', restrictNumeric); |
425 | 444 | }; |
426 | 445 | payform.parseCardExpiry = function(value) { |
427 | | - var month, prefix, year, _ref; |
| 446 | + var month, prefix, ref, year; |
428 | 447 | value = value.replace(/\s/g, ''); |
429 | | - _ref = value.split('/', 2), month = _ref[0], year = _ref[1]; |
| 448 | + ref = value.split('/', 2), month = ref[0], year = ref[1]; |
430 | 449 | if ((year != null ? year.length : void 0) === 2 && /^\d+$/.test(year)) { |
431 | 450 | prefix = (new Date).getFullYear(); |
432 | 451 | prefix = prefix.toString().slice(0, 2); |
|
440 | 459 | }; |
441 | 460 | }; |
442 | 461 | payform.validateCardNumber = function(num) { |
443 | | - var card, _ref; |
| 462 | + var card, ref; |
444 | 463 | num = (num + '').replace(/\s+|-/g, ''); |
445 | 464 | if (!/^\d+$/.test(num)) { |
446 | 465 | return false; |
|
449 | 468 | if (!card) { |
450 | 469 | return false; |
451 | 470 | } |
452 | | - return (_ref = num.length, __indexOf.call(card.length, _ref) >= 0) && (card.luhn === false || luhnCheck(num)); |
| 471 | + return (ref = num.length, indexOf.call(card.length, ref) >= 0) && (card.luhn === false || luhnCheck(num)); |
453 | 472 | }; |
454 | 473 | payform.validateCardExpiry = function(month, year) { |
455 | | - var currentTime, expiry, _ref; |
| 474 | + var currentTime, expiry, ref; |
456 | 475 | if (typeof month === 'object' && 'month' in month) { |
457 | | - _ref = month, month = _ref.month, year = _ref.year; |
| 476 | + ref = month, month = ref.month, year = ref.year; |
458 | 477 | } |
459 | 478 | if (!(month && year)) { |
460 | 479 | return false; |
|
487 | 506 | return expiry > currentTime; |
488 | 507 | }; |
489 | 508 | payform.validateCardCVC = function(cvc, type) { |
490 | | - var card, _ref; |
| 509 | + var card, ref; |
491 | 510 | cvc = String(cvc).trim(); |
492 | 511 | if (!/^\d+$/.test(cvc)) { |
493 | 512 | return false; |
494 | 513 | } |
495 | 514 | card = cardFromType(type); |
496 | 515 | if (card != null) { |
497 | | - return _ref = cvc.length, __indexOf.call(card.cvcLength, _ref) >= 0; |
| 516 | + return ref = cvc.length, indexOf.call(card.cvcLength, ref) >= 0; |
498 | 517 | } else { |
499 | 518 | return cvc.length >= 3 && cvc.length <= 4; |
500 | 519 | } |
501 | 520 | }; |
502 | 521 | payform.parseCardType = function(num) { |
503 | | - var _ref; |
| 522 | + var ref; |
504 | 523 | if (!num) { |
505 | 524 | return null; |
506 | 525 | } |
507 | | - return ((_ref = cardFromNumber(num)) != null ? _ref.type : void 0) || null; |
| 526 | + return ((ref = cardFromNumber(num)) != null ? ref.type : void 0) || null; |
508 | 527 | }; |
509 | 528 | payform.formatCardNumber = function(num) { |
510 | | - var card, groups, upperLength, _ref; |
| 529 | + var card, groups, ref, upperLength; |
| 530 | + num = replaceFullWidthChars(num); |
511 | 531 | num = num.replace(/\D/g, ''); |
512 | 532 | card = cardFromNumber(num); |
513 | 533 | if (!card) { |
|
516 | 536 | upperLength = card.length[card.length.length - 1]; |
517 | 537 | num = num.slice(0, upperLength); |
518 | 538 | if (card.format.global) { |
519 | | - return (_ref = num.match(card.format)) != null ? _ref.join(' ') : void 0; |
| 539 | + return (ref = num.match(card.format)) != null ? ref.join(' ') : void 0; |
520 | 540 | } else { |
521 | 541 | groups = card.format.exec(num); |
522 | 542 | if (groups == null) { |
|
529 | 549 | }; |
530 | 550 | payform.formatCardExpiry = function(expiry) { |
531 | 551 | var mon, parts, sep, year; |
| 552 | + expiry = replaceFullWidthChars(expiry); |
532 | 553 | parts = expiry.match(/^\D*(\d{1,2})(\D+)?(\d{1,4})?/); |
533 | 554 | if (!parts) { |
534 | 555 | return ''; |
|
0 commit comments