forked from PawelDecowski/jquery-creditcardvalidator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.creditCardValidator.js
220 lines (211 loc) · 6.44 KB
/
jquery.creditCardValidator.js
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Generated by CoffeeScript 1.10.0
/*
jQuery Credit Card Validator 1.0
Copyright 2012-2015 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.
*/
(function() {
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; };
$ = jQuery;
$.fn.validateCreditCard = function(callback, options) {
var bind, card, card_type, card_types, get_card_type, i, is_valid_length, is_valid_luhn, len, normalize, ref, validate, validate_number;
card_types = [
{
name: 'amex',
pattern: /^3[47]/,
valid_length: [15]
}, {
name: 'diners_club_carte_blanche',
pattern: /^30[0-5]/,
valid_length: [14]
}, {
name: 'diners_club_international',
pattern: /^36/,
valid_length: [14]
}, {
name: 'jcb',
pattern: /^35(2[89]|[3-8][0-9])/,
valid_length: [16]
}, {
name: 'laser',
pattern: /^(6304|670[69]|6771)/,
valid_length: [16, 17, 18, 19]
}, {
name: 'visa_electron',
pattern: /^(4026|417500|4508|4844|491(3|7))/,
valid_length: [16]
}, {
name: 'visa',
pattern: /^4/,
valid_length: [16]
}, {
name: 'mastercard',
pattern: /^5[1-5]/,
valid_length: [16]
}, {
name: 'maestro',
pattern: /^(5018|5020|5038|6304|6759|676[1-3])/,
valid_length: [12, 13, 14, 15, 16, 17, 18, 19]
}, {
name: 'discover',
pattern: /^(6011|622(12[6-9]|1[3-9][0-9]|[2-8][0-9]{2}|9[0-1][0-9]|92[0-5]|64[4-9])|65)/,
valid_length: [16]
}, {
name: 'jcp',
pattern: /\d{11}/,
valid_length: [11]
}, {
name: 'nordstroms',
pattern: /\d{8}?\d{9}/,
valid_length: [8, 9]
}, {
name: 'nordstroms',
pattern: /^(5048878\d{9})$/,
valid_length: [16]
}
];
bind = false;
if (callback) {
if (typeof callback === 'object') {
options = callback;
bind = false;
callback = null;
} else if (typeof callback === 'function') {
bind = true;
}
}
if (options == null) {
options = {};
}
if (options.accept == null) {
options.accept = (function() {
var i, len, results;
results = [];
for (i = 0, len = card_types.length; i < len; i++) {
card = card_types[i];
results.push(card.name);
}
return results;
})();
}
ref = options.accept;
for (i = 0, len = ref.length; i < len; i++) {
card_type = ref[i];
if (indexOf.call((function() {
var j, len1, results;
results = [];
for (j = 0, len1 = card_types.length; j < len1; j++) {
card = card_types[j];
results.push(card.name);
}
return results;
})(), card_type) < 0) {
throw "Credit card type '" + card_type + "' is not supported";
}
}
get_card_type = function(number) {
var j, len1, ref1;
ref1 = (function() {
var k, len1, ref1, results;
results = [];
for (k = 0, len1 = card_types.length; k < len1; k++) {
card = card_types[k];
if (ref1 = card.name, indexOf.call(options.accept, ref1) >= 0) {
results.push(card);
}
}
return results;
})();
for (j = 0, len1 = ref1.length; j < len1; j++) {
card_type = ref1[j];
if (number.match(card_type.pattern)) {
return card_type;
}
}
return null;
};
is_valid_luhn = function(number) {
var digit, j, len1, n, ref1, sum;
sum = 0;
ref1 = number.split('').reverse();
for (n = j = 0, len1 = ref1.length; j < len1; n = ++j) {
digit = ref1[n];
digit = +digit;
if (n % 2) {
digit *= 2;
if (digit < 10) {
sum += digit;
} else {
sum += digit - 9;
}
} else {
sum += digit;
}
}
return sum % 10 === 0;
};
is_valid_length = function(number, card_type) {
var ref1;
return ref1 = number.length, indexOf.call(card_type.valid_length, ref1) >= 0;
};
validate_number = (function(_this) {
return function(number) {
var length_valid, luhn_valid;
card_type = get_card_type(number);
luhn_valid = false;
length_valid = false;
if (card_type != null) {
luhn_valid = is_valid_luhn(number);
length_valid = is_valid_length(number, card_type);
}
return {
card_type: card_type,
valid: luhn_valid && length_valid,
luhn_valid: luhn_valid,
length_valid: length_valid
};
};
})(this);
validate = (function(_this) {
return function() {
var number;
number = normalize($(_this).val());
return validate_number(number);
};
})(this);
normalize = function(number) {
return number.replace(/[ -]/g, '');
};
if (!bind) {
return validate();
}
this.on('input.jccv', (function(_this) {
return function() {
$(_this).off('keyup.jccv');
return callback.call(_this, validate());
};
})(this));
this.on('keyup.jccv', (function(_this) {
return function() {
return callback.call(_this, validate());
};
})(this));
callback.call(this, validate());
return this;
};
}).call(this);