This repository has been archived by the owner on Sep 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
card.coffee
131 lines (117 loc) · 2.7 KB
/
card.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
defaultFormat = /(\d{1,4})/g
cards = [
type: 'amex'
pattern: /^3[47]/
format: /(\d{1,4})(\d{1,6})?(\d{1,5})?/
length: [15]
cvcLength: [4]
luhn: true
,
type: 'dankort'
pattern: /^5019/
format: defaultFormat
length: [16]
cvcLength: [3]
luhn: true
,
type: 'dinersclub'
pattern: /^(36|38|30[0-5])/
format: /(\d{1,4})(\d{1,6})?(\d{1,4})?/
length: [14]
cvcLength: [3]
luhn: true
,
type: 'discover'
pattern: /^(6011|65|64[4-9]|622)/
format: defaultFormat
length: [16]
cvcLength: [3]
luhn: true
,
type: 'jcb'
pattern: /^35/
format: defaultFormat
length: [16]
cvcLength: [3]
luhn: true
,
type: 'laser'
pattern: /^(6706|6771|6709)/
format: defaultFormat
length: [16..19]
cvcLength: [3]
luhn: true
,
type: 'maestro'
pattern: /^(5018|5020|5038|6304|6703|6708|6759|676[1-3])/
format: defaultFormat
length: [12..19]
cvcLength: [3]
luhn: true
,
type: 'mastercard'
pattern: /^(5[1-5]|677189)|^(222[1-9]|2[3-6]\d{2}|27[0-1]\d|2720)/
format: defaultFormat
length: [16]
cvcLength: [3]
luhn: true
,
type: 'unionpay'
pattern: /^62/
format: defaultFormat
length: [16..19]
cvcLength: [3]
luhn: false
,
type: 'visaelectron'
pattern: /^4(026|17500|405|508|844|91[37])/
format: defaultFormat
length: [16]
cvcLength: [3]
luhn: true
,
type: 'elo'
pattern: /^(4011|438935|45(1416|76|7393)|50(4175|6699|67|90[4-7])|63(6297|6368))/
format: defaultFormat
length: [16]
cvcLength: [3]
luhn: true
,
type: 'visa'
pattern: /^4/
format: defaultFormat
length: [13, 16, 19]
cvcLength: [3]
luhn: true
]
export luhnCheck = (num) ->
odd = true
sum = 0
digits = (num + '').split('').reverse()
for digit in digits
digit = parseInt(digit, 10)
digit *= 2 if (odd = !odd)
digit -= 9 if digit > 9
sum += digit
sum % 10 == 0
export cardFromNumber = (num) ->
num = (num + '').replace(/\D/g, '')
return card for card in cards when card.pattern.test(num)
export cardType = (num) ->
return null unless num
cardFromNumber(num)?.type or null
export restrictNumeric = (e) ->
# Key event is for a browser shortcut
return true if e.metaKey or e.ctrlKey
key = e.keyCode
# If keycode is a space
return e.preventDefault() if key is 32
# If keycode is a special char (WebKit)
return true if key is 0
# If char is a special char (Firefox)
return true if key < 33
input = String.fromCharCode key
# Char is a number or a space
if !/[\d\s]/.test input
return e.preventDefault()
return true