Permalink
Browse files

Initiatialise empty options param if undefined

  • Loading branch information...
1 parent 7f126ae commit 177132a70c146bdc05a8cde295876b904d207556 @PawelDecowski committed Feb 7, 2013
Showing with 3 additions and 163 deletions.
  1. +2 −0 jquery.creditCardValidator.coffee
  2. +1 −163 jquery.creditCardValidator.js
@@ -70,6 +70,8 @@ $.fn.validateCreditCard = (callback, options) ->
}
]
+ options ?= {}
+
options.accept ?= (card.name for card in card_types)
for card_type in options.accept
@@ -1,5 +1,4 @@
// Generated by CoffeeScript 1.4.0
-
/*
jQuery Credit Card Validator
@@ -14,165 +13,4 @@ or send a letter to:
Creative Commons, 444 Castro Street, Suite 900,
Mountain View, California, 94041, USA.
-*/
-
-
-(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 card, card_type, card_types, get_card_type, is_valid_length, is_valid_luhn, normalize, validate, validate_number, _i, _len, _ref, _ref1;
- 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]
- }
- ];
- if ((_ref = 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;
- })();
- }
- _ref1 = options.accept;
- for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
- card_type = _ref1[_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, _ref2;
- _ref2 = (function() {
- var _k, _len1, _ref2, _results;
- _results = [];
- for (_k = 0, _len1 = card_types.length; _k < _len1; _k++) {
- card = card_types[_k];
- if (_ref2 = card.name, __indexOf.call(options.accept, _ref2) >= 0) {
- _results.push(card);
- }
- }
- return _results;
- })();
- for (_j = 0, _len1 = _ref2.length; _j < _len1; _j++) {
- card_type = _ref2[_j];
- if (number.match(card_type.pattern)) {
- return card_type;
- }
- }
- return null;
- };
- is_valid_luhn = function(number) {
- var digit, n, sum, _j, _len1, _ref2;
- sum = 0;
- _ref2 = number.split('').reverse();
- for (n = _j = 0, _len1 = _ref2.length; _j < _len1; n = ++_j) {
- digit = _ref2[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 _ref2;
- return _ref2 = number.length, __indexOf.call(card_type.valid_length, _ref2) >= 0;
- };
- validate_number = 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 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);
- });
- if (this.length !== 0) {
- validate.call(this);
- }
- return this;
- };
-
-}).call(this);
+*/(function(){var e,t=[].indexOf||function(e){for(var t=0,n=this.length;t<n;t++)if(t in this&&this[t]===e)return t;return-1};e=jQuery;e.fn.validateCreditCard=function(n,r){var i,s,o,u,a,f,l,c,h,p,d,v,m;o=[{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]}];r==null&&(r={});(v=r.accept)==null&&(r.accept=function(){var e,t,n;n=[];for(e=0,t=o.length;e<t;e++){i=o[e];n.push(i.name)}return n}());m=r.accept;for(p=0,d=m.length;p<d;p++){s=m[p];if(t.call(function(){var e,t,n;n=[];for(e=0,t=o.length;e<t;e++){i=o[e];n.push(i.name)}return n}(),s)<0)throw"Credit card type '"+s+"' is not supported"}u=function(e){var n,u,a;a=function(){var e,n,s,u;u=[];for(e=0,n=o.length;e<n;e++){i=o[e];(s=i.name,t.call(r.accept,s)>=0)&&u.push(i)}return u}();for(n=0,u=a.length;n<u;n++){s=a[n];if(e.match(s.pattern))return s}return null};f=function(e){var t,n,r,i,s,o;r=0;o=e.split("").reverse();for(n=i=0,s=o.length;i<s;n=++i){t=o[n];t=+t;if(n%2){t*=2;t<10?r+=t:r+=t-9}else r+=t}return r%10===0};a=function(e,n){var r;return r=e.length,t.call(n.valid_length,r)>=0};h=function(e){var t,r;s=u(e);r=!1;t=!1;if(s!=null){r=f(e);t=a(e,s)}return n({card_type:s,luhn_valid:r,length_valid:t})};c=function(){var t;t=l(e(this).val());return h(t)};l=function(e){return e.replace(/[ -]/g,"")};this.bind("input",function(){e(this).unbind("keyup");return c.call(this)});this.bind("keyup",function(){return c.call(this)});this.length!==0&&c.call(this);return this}}).call(this);

0 comments on commit 177132a

Please sign in to comment.