forked from PawelDecowski/jquery-creditcardvalidator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.creditCardValidator.js
More file actions
139 lines (129 loc) · 3.75 KB
/
Copy pathjquery.creditCardValidator.js
File metadata and controls
139 lines (129 loc) · 3.75 KB
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
/*
jQuery Credit Card Validator
Copyright 2012 Pawel Decowski
This work is licensed under the Creative Commons Attribution-ShareAlike 3.0
Unported License. To view a copy of this license, visit:
http://creativecommons.org/licenses/by-sa/3.0/
or send a letter to:
Creative Commons, 444 Castro Street, Suite 900,
Mountain View, California, 94041, USA.
*/
(function() {
var $,
__indexOf = Array.prototype.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) {
var card_types, get_card_type, is_valid_length, is_valid_luhn, normalize, 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: 'diners_club_us_and_ca',
pattern: /^5[45]/,
valid_length: [16]
}, {
name: 'jcb',
pattern: /^35(2[89]|[3-8][0-9])/,
valid_length: [16]
}, {
name: 'laser',
pattern: /^(6304|630[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: [13, 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]
}
];
get_card_type = function(number) {
var card_type, _i, _len;
for (_i = 0, _len = card_types.length; _i < _len; _i++) {
card_type = card_types[_i];
if (number.match(card_type.pattern)) return card_type;
}
return null;
};
is_valid_luhn = function(number) {
var digit, n, sum, _len, _ref;
sum = 0;
_ref = number.split('').reverse().join('');
for (n = 0, _len = _ref.length; n < _len; n++) {
digit = _ref[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 _ref;
return _ref = number.length, __indexOf.call(card_type.valid_length, _ref) >= 0;
};
validate_number = function(number) {
var card_type, 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 callback({
card_type: card_type,
luhn_valid: luhn_valid,
length_valid: length_valid
});
};
validate = function() {
var number;
number = normalize($(this).val());
return validate_number(number);
};
normalize = function(number) {
return number.replace(/[ -]/g, '');
};
this.bind('input', function() {
$(this).unbind('keyup');
return validate.call(this);
});
this.bind('keyup', function() {
return validate.call(this);
});
validate.call(this);
return this;
};
}).call(this);