forked from PawelDecowski/jquery-creditcardvalidator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.creditCardValidator.coffee
176 lines (148 loc) · 4.92 KB
/
jquery.creditCardValidator.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
###
jQuery Credit Card Validator 1.2
Copyright 2012 Pawel Decowski
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
###
$ = jQuery
$.fn.validateCreditCard = (callback, options) ->
card_types = [
{
name: 'amex'
range: '34,37'
valid_length: [ 15 ]
}
{
name: 'diners_club_carte_blanche'
range: '300-305'
valid_length: [ 16..19 ]
}
{
name: 'diners_club_international'
range: '3095, 36, 38-39'
valid_length: [ 14..19 ]
}
{
name: 'jcb'
range: '3088-3094, 3096-3102, 3112-3120, 3158-3159, 3337-3349, 3528-3589'
valid_length: [ 16 ]
}
{
name: 'laser'
range: '6304, 6706, 6709, 6771'
valid_length: [ 16..19 ]
}
{
name: 'visa_electron'
range: '4026, 417500, 4508, 4844, 4913, 4917'
valid_length: [ 16 ]
}
{
name: 'visa'
range: '4'
valid_length: [ 13..19 ]
}
{
name: 'mastercard'
range: '51-55,2221-2720'
valid_length: [ 16 ]
}
{
name: 'discover'
range: '6011, 622126-622925, 644-649, 65'
valid_length: [ 16..19 ]
}
{
name: 'dankort'
range: '5019'
valid_length: [ 16 ]
}
{
name: 'maestro'
range: '50, 56-69'
valid_length: [ 12..19 ]
}
{
name: 'uatp'
range: '1'
valid_length: [ 15 ]
}
{
name: 'mir'
range: '2200-2204'
valid_length: [ 16 ]
}
]
bind = false
if callback
if typeof callback == 'object'
# callback has been skipped and only options parameter has been passed
options = callback
bind = false
callback = null
else if typeof callback == 'function'
bind = true
options ?= {}
options.accept ?= (card.name for card in card_types)
for card_type in options.accept
if card_type not in (card.name for card in card_types)
throw Error "Credit card type '#{ card_type }' is not supported"
get_card_type = (number) ->
for card_type in (card for card in card_types when card.name in options.accept)
r = Range.rangeWithString(card_type.range)
if r.match(number)
return card_type
null
is_valid_luhn = (number) ->
sum = 0
for digit, n in number.split('').reverse()
digit = +digit # the + casts the string to int
if n % 2
digit *= 2
if digit < 10 then sum += digit else sum += digit - 9
else
sum += digit
sum % 10 == 0
is_valid_length = (number, card_type) ->
number.length in card_type.valid_length
validate_number = (number) ->
card_type = get_card_type number
luhn_valid = is_valid_luhn number
length_valid = false
if card_type?
length_valid = is_valid_length number, card_type
card_type: card_type
valid: luhn_valid and length_valid
luhn_valid: luhn_valid
length_valid: length_valid
validate = =>
number = normalize $(this).val()
validate_number number
normalize = (number) ->
number.replace /[ -]/g, ''
if not bind
return validate()
this.on('input.jccv', =>
$(this).off('keyup.jccv') # if input event is fired (so is supported) then unbind keyup
callback.call this, validate()
)
# bind keyup in case input event isn't supported
this.on('keyup.jccv', =>
callback.call this, validate()
)
# run validation straight away in case the card number is prefilled
callback.call this, validate()
this