jquery.creditCardValidator.js 6.2 KB

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