vendor: 4,284 bytes, lines 1-149
1// Generated by CoffeeScript 1.4.0 2 3/* 4jQuery Credit Card Validator 5 6Copyright 2012 Pawel Decowski 7 8This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 9Unported License. To view a copy of this license, visit: 10 11http://creativecommons.org/licenses/by-sa/3.0/ 12 13or send a letter to: 14 15Creative Commons, 444 Castro Street, Suite 900, 16Mountain View, California, 94041, USA. 17*/ 18 19 20(function() { 21 var $, 22 __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; }; 23 24 $ = jQuery; 25 26 $.fn.validateCreditCard = function(callback, options) { 27 var card, card_type, card_types, get_card_type, is_valid_length, is_valid_luhn, normalize, validate, validate_number, _i, _len, _ref, _ref1; 28 29 card_types = [ 30 { 31 name: 'amex', 32 pattern: /^3[47]/, 33 valid_length: [15] 34 }, { 35 name: 'diners_club_carte_blanche', 36 pattern: /^30[0-5]/, 37 valid_length: [14] 38 }, { 39 name: 'diners_club_international', 40 pattern: /^36/, 41 valid_length: [14] 42 }, { 43 name: 'jcb', 44 pattern: /^35(2[89]|[3-8][0-9])/, 45 valid_length: [16] 46 }, { 47 name: 'laser', 48 pattern: /^(6304|670[69]|6771)/, 49 valid_length: [16, 17, 18, 19] 50 }, { 51 name: 'visa_electron', 52 pattern: /^(4026|417500|4508|4844|491(3|7))/, 53 valid_length: [16] 54 }, { 55 name: 'visa', 56 pattern: /^4/, 57 valid_length: [16] 58 }, { 59 name: 'mastercard', 60 pattern: /^5[1-5]/, 61 valid_length: [16] 62 }, { 63 name: 'maestro', 64 pattern: /^(5018|5020|5038|6304|6759|676[1-3])/, 65 valid_length: [12, 13, 14, 15, 16, 17, 18, 19] 66 }, { 67 name: 'discover', 68 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)/, 69 valid_length: [16] 70 } 71 ]; 72 if (options == null) { 73 options = {}; 74 } 75 if ((_ref = options.accept) == null) { 76 options.accept = (function() { 77 var _i, _len, _results; 78 _results = []; 79 for (_i = 0, _len = card_types.length; _i < _len; _i++) { 80 card = card_types[_i]; 81 _results.push(card.name); 82 } 83 return _results; 84 })(); 85 } 86 _ref1 = options.accept; 87 for (_i = 0, _len = _ref1.length; _i < _len; _i++) { 88 card_type = _ref1[_i]; 89 if (__indexOf.call((function() { 90 var _j, _len1, _results; 91 _results = []; 92 for (_j = 0, _len1 = card_types.length; _j < _len1; _j++) { 93 card = card_types[_j]; 94 _results.push(card.name); 95 } 96 return _results; 97 })(), card_type) < 0) { 98 throw "Credit card type '" + card_type + "' is not supported"; 99 } 100 } 101 get_card_type = function(number) { 102 var _j, _len1, _ref2; 103 _ref2 = (function() { 104 var _k, _len1, _ref2, _results; 105 _results = []; 106 for (_k = 0, _len1 = card_types.length; _k < _len1; _k++) { 107 card = card_types[_k]; 108 if (_ref2 = card.name, __indexOf.call(options.accept, _ref2) >= 0) { 109 _results.push(card); 110 } 111 } 112 return _results; 113 })(); 114 for (_j = 0, _len1 = _ref2.length; _j < _len1; _j++) { 115 card_type = _ref2[_j]; 116 if (number.match(card_type.pattern)) { 117 return card_type; 118 } 119 } 120 return null; 121 }; 122 is_valid_luhn = function(number) { 123 var digit, n, sum, _j, _len1, _ref2; 124 sum = 0; 125 _ref2 = number.split('').reverse(); 126 for (n = _j = 0, _len1 = _ref2.length; _j < _len1; n = ++_j) { 127 digit = _ref2[n]; 128 digit = +digit; 129 if (n % 2) { 130 digit *= 2; 131 if (digit < 10) { 132 sum += digit; 133 } else { 134 sum += digit - 9; 135 } 136 } else { 137 sum += digit; 138 } 139 } 140 return sum % 10 === 0; 141 }; 142 is_valid_length = function(number, card_type) { 143 var _ref2; 144 return _ref2 = number.length, __indexOf.call(card_type.valid_length, _ref2) >= 0; 145 }; 146 validate_number = function(number) { 147 var length_valid, luhn_valid; 148 card_type = get_card_type(number); 149 luhn_valid = false;
150 length_valid = false; 151 if (card_type != null) { 152 luhn_valid = is_valid_luhn(number); 153 length_valid = is_valid_length(number, card_type); 154 } 155 return callback({ 156 card_type: card_type, 157 luhn_valid: luhn_valid, 158 length_valid: length_valid 159 }); 160 }; 161 validate = function() { 162 var number; 163 number = normalize($(this).val()); 164 return validate_number(number); 165 }; 166 normalize = function(number) { 167 return number.replace(/[ -]/g, ''); 168 }; 169 this.bind('input', function() { 170 $(this).unbind('keyup'); 171 return validate.call(this); 172 }); 173 this.bind('keyup', function() { 174 return validate.call(this); 175 }); 176 if (this.length !== 0) { 177 validate.call(this); 178 } 179 180 this.validate_number_direct = function(number) { 181 var length_valid, luhn_valid; 182 card_type = get_card_type(number); 183 luhn_valid = false; 184 length_valid = false; 185 if (card_type != null) { 186 luhn_valid = is_valid_luhn(number); 187 length_valid = is_valid_length(number, card_type); 188 } 189 return { 190 card_type: card_type, 191 luhn_valid: luhn_valid, 192 length_valid: length_valid 193 }; 194 }; 195 196 return this; 197 }; 198 199}).call(this);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.