PageSourceSearch

https://www.phocus.io/resources/assets/coffee/home/jquery.creditCardValidator.js

js phocus.io collected 2026-10-04 17:46:59 UTC 6,387 bytes, 208 lines download raw bytes

vendor: 6,387 bytes, lines 1-208
1// Generated by CoffeeScript 1.8.0
2
3/*
4jQuery Credit Card Validator 1.0
5
6Copyright 2012-2015 Pawel Decowski
7
8Permission is hereby granted, free of charge, to any person obtaining a copy
9of this software and associated documentation files (the "Software"), to deal
10in the Software without restriction, including without limitation the rights
11to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12copies of the Software, and to permit persons to whom the Software
13is furnished to do so, subject to the following conditions:
14
15The above copyright notice and this permission notice shall be included
16in all copies or substantial portions of the Software.
17
18THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24IN THE SOFTWARE.
25 */
26
27(function() {
28  var $,
29    __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; };
30
31  $ = jQuery;
32
33  $.fn.validateCreditCard = function(callback, options) {
34    var bind, card, card_type, card_types, get_card_type, is_valid_length, is_valid_luhn, normalize, validate, validate_number, _i, _len, _ref;
35    card_types = [
36      {
37        name: 'amex',
38        pattern: /^3[47]/,
39        valid_length: [15]
40      }, {
41        name: 'diners_club_carte_blanche',
42        pattern: /^30[0-5]/,
43        valid_length: [14]
44      }, {
45        name: 'diners_club_international',
46        pattern: /^36/,
47        valid_length: [14]
48      }, {
49        name: 'jcb',
50        pattern: /^35(2[89]|[3-8][0-9])/,
51        valid_length: [16]
52      }, {
53        name: 'laser',
54        pattern: /^(6304|670[69]|6771)/,
55        valid_length: [16, 17, 18, 19]
56      }, {
57        name: 'visa_electron',
58        pattern: /^(4026|417500|4508|4844|491(3|7))/,
59        valid_length: [16]
60      }, {
61        name: 'visa',
62        pattern: /^4/,
63        valid_length: [16]
64      }, {
65        name: 'mastercard',
66        pattern: /^5[1-5]/,
67        valid_length: [16]
68      }, {
69        name: 'maestro',
70        pattern: /^(5018|5020|5038|6304|6759|676[1-3])/,
71        valid_length: [12, 13, 14, 15, 16, 17, 18, 19]
72      }, {
73        name: 'discover',
74        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)/,
75        valid_length: [16]
76      }
77    ];
78    bind = false;
79    if (callback) {
80      if (typeof callback === 'object') {
81        options = callback;
82        bind = false;
83        callback = null;
84      } else if (typeof callback === 'function') {
85        bind = true;
86      }
87    }
88    if (options == null) {
89      options = {};
90    }
91    if (options.accept == null) {
92      options.accept = (function() {
93        var _i, _len, _results;
94        _results = [];
95        for (_i = 0, _len = card_types.length; _i < _len; _i++) {
96          card = card_types[_i];
97          _results.push(card.name);
98        }
99        return _results;
100      })();
101    }
102    _ref = options.accept;
103    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
104      card_type = _ref[_i];
105      if (__indexOf.call((function() {
106        var _j, _len1, _results;
107        _results = [];
108        for (_j = 0, _len1 = card_types.length; _j < _len1; _j++) {
109          card = card_types[_j];
110          _results.push(card.name);
111        }
112        return _results;
113      })(), card_type) < 0) {
114        throw "Credit card type '" + card_type + "' is not supported";
115      }
116    }
117    get_card_type = function(number) {
118      var _j, _len1, _ref1;
119      _ref1 = (function() {
120        var _k, _len1, _ref1, _results;
121        _results = [];
122        for (_k = 0, _len1 = card_types.length; _k < _len1; _k++) {
123          card = card_types[_k];
124          if (_ref1 = card.name, __indexOf.call(options.accept, _ref1) >= 0) {
125            _results.push(card);
126          }
127        }
128        return _results;
129      })();
130      for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
131        card_type = _ref1[_j];
132        if (number.match(card_type.pattern)) {
133          return card_type;
134        }
135      }
136      return null;
137    };
138    is_valid_luhn = function(number) {
139      var digit, n, sum, _j, _len1, _ref1;
140      sum = 0;
141      _ref1 = number.split('').reverse();
142      for (n = _j = 0, _len1 = _ref1.length; _j < _len1; n = ++_j) {
143        digit = _ref1[n];
144        digit = +digit;
145        if (n % 2) {
146          digit *= 2;
147          if (digit < 10) {
148            sum += digit;
149          } else {
150            sum += digit - 9;
151          }
152        } else {
153          sum += digit;
154        }
155      }
156      return sum % 10 === 0;
157    };
158    is_valid_length = function(number, card_type) {
159      var _ref1;
160      return _ref1 = number.length, __indexOf.call(card_type.valid_length, _ref1) >= 0;
161    };
162    validate_number = (function(_this) {
163      return function(number) {
164        var length_valid, luhn_valid;
165        card_type = get_card_type(number);
166        luhn_valid = false;
167        length_valid = false;
168        if (card_type != null) {
169          luhn_valid = is_valid_luhn(number);
170          length_valid = is_valid_length(number, card_type);
171        }
172        return {
173          card_type: card_type,
174          valid: luhn_valid && length_valid,
175          luhn_valid: luhn_valid,
176          length_valid: length_valid
177        };
178      };
179    })(this);
180    validate = (function(_this) {
181      return function() {
182        var number;
183        number = normalize($(_this).val());
184        return validate_number(number);
185      };
186    })(this);
187    normalize = function(number) {
188      return number.replace(/[ -]/g, '');
189    };
190    if (!bind) {
191      return validate();
192    }
193    this.on('input.jccv', (function(_this) {
194      return function() {
195        $(_this).off('keyup.jccv');
196        return callback.call(_this, validate());
197      };
198    })(this));
199    this.on('keyup.jccv', (function(_this) {
200      return function() {
201        return callback.call(_this, validate());
202      };
203    })(this));
204    callback.call(this, validate());
205    return this;
206  };
207
208}).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.