From 2a9aa75577bbb23e078d65c45d02fa0089a57056 Mon Sep 17 00:00:00 2001 From: Omar Shammas Date: Thu, 30 May 2013 02:13:33 -0400 Subject: [PATCH] Restructuing and seperating files making it easier and cleaner to add new fields --- lib/fields/credit_card_cvc.js | 20 +- lib/fields/credit_card_expiry.js | 24 +- lib/fields/credit_card_number.js | 20 +- lib/fields/date.js | 68 ++- lib/fields/number.js | 16 +- .../ontario_photo_health_card_number.js | 44 +- lib/fields/phone_number.js | 39 +- lib/fields/postal_code.js | 42 +- lib/jquery.formance.js | 52 +- lib/jquery.formatters.js | 427 -------------- lib/jquery.payment.js | 522 ------------------ lib/jquery.validators.js | 105 ---- src/fields/credit_card_cvc.coffee | 17 +- src/fields/credit_card_expiry.coffee | 18 +- src/fields/credit_card_number.coffee | 14 +- src/fields/date.coffee | 43 +- src/fields/number.coffee | 13 +- .../ontario_photo_health_card_number.coffee | 34 +- src/fields/phone_number.coffee | 30 +- src/fields/postal_code.coffee | 41 +- src/jquery.formance.coffee | 39 +- src/jquery.formatters.coffee | 352 ------------ src/jquery.payment.coffee | 439 --------------- src/jquery.validators.coffee | 87 --- test/fields/credit_card_cvc.coffee | 58 ++ test/fields/credit_card_expiry.coffee | 108 ++++ test/fields/credit_card_number.coffee | 137 +++++ test/fields/date.coffee | 167 ++++++ test/fields/number.coffee | 54 ++ .../ontario_photo_health_card_number.coffee | 78 +++ test/fields/phone_number.coffee | 88 +++ test/fields/postal_code.coffee | 82 +++ test/formatters.coffee | 213 ------- ...formance.coffee => jquery.formance.coffee} | 34 +- test/payment.coffee | 267 --------- test/untitled | 12 - test/validators.coffee | 239 -------- 37 files changed, 1091 insertions(+), 2952 deletions(-) delete mode 100644 lib/jquery.formatters.js delete mode 100644 lib/jquery.payment.js delete mode 100644 lib/jquery.validators.js delete mode 100644 src/jquery.formatters.coffee delete mode 100644 src/jquery.payment.coffee delete mode 100644 src/jquery.validators.coffee create mode 100644 test/fields/credit_card_cvc.coffee create mode 100644 test/fields/credit_card_expiry.coffee create mode 100644 test/fields/credit_card_number.coffee create mode 100644 test/fields/date.coffee create mode 100644 test/fields/number.coffee create mode 100644 test/fields/ontario_photo_health_card_number.coffee create mode 100644 test/fields/phone_number.coffee create mode 100644 test/fields/postal_code.coffee delete mode 100644 test/formatters.coffee rename test/{formance.coffee => jquery.formance.coffee} (55%) delete mode 100644 test/payment.coffee delete mode 100644 test/untitled delete mode 100644 test/validators.coffee diff --git a/lib/fields/credit_card_cvc.js b/lib/fields/credit_card_cvc.js index 49f9f85..f25e290 100644 --- a/lib/fields/credit_card_cvc.js +++ b/lib/fields/credit_card_cvc.js @@ -6,21 +6,13 @@ $ = jQuery; - $.fn.formance.creditCardCVC = { - format: function() { - this.payment('formatCardExpiry'); - return this; - }, - validate: function() { - var type, val; + $.formance.fn.formatCreditCardCVC = function() { + this.payment('formatCardCVC'); + return this; + }; - val = $(this).val(); - type = null; - return $.payment.validateCardCVC(val, type); - }, - parse: function(expiryString) { - return $.payment.cardExpiryVal(expiryString); - } + $.formance.validateCreditCardCVC = function(val, type) { + return $.payment.validateCardCVC(val, type); }; }).call(this); diff --git a/lib/fields/credit_card_expiry.js b/lib/fields/credit_card_expiry.js index 49dd340..862f252 100644 --- a/lib/fields/credit_card_expiry.js +++ b/lib/fields/credit_card_expiry.js @@ -6,21 +6,17 @@ $ = jQuery; - $.fn.formance.creditCardExpiry = { - format: function() { - this.payment('formatCardExpiry'); - return this; - }, - validate: function() { - var expiry, val; + $.formance.fn.formatCreditCardExpiry = function() { + this.payment('formatCardExpiry'); + return this; + }; + + $.formance.validateCreditCardExpiry = function(month, year) { + return $.payment.validateCardExpiry(month, year); + }; - val = $(this).val(); - expiry = this.parse(val); - return $.payment.validateCardExpiry(expiry.month, expiry.year); - }, - parse: function(expiryString) { - return $.payment.cardExpiryVal(expiryString); - } + $.formance.creditCardExpiryVal = function(expiryString) { + return $.payment.cardExpiryVal(expiryString); }; }).call(this); diff --git a/lib/fields/credit_card_number.js b/lib/fields/credit_card_number.js index d206c61..e0cad5a 100644 --- a/lib/fields/credit_card_number.js +++ b/lib/fields/credit_card_number.js @@ -6,17 +6,17 @@ $ = jQuery; - $.fn.formance.creditCardNumber = { - format: function() { - this.payment('formatCardNumber'); - return this; - }, - validate: function() { - var val; + $.formance.fn.formatCreditCardNumber = function() { + this.payment('formatCardNumber'); + return this; + }; + + $.formance.validateCreditCardNumber = function(num) { + return $.payment.validateCardNumber(num); + }; - val = $(this).val(); - return $.payment.validateCardNumber(num); - } + $.formance.creditCardType = function(num) { + return $.payment.cardType(num); }; }).call(this); diff --git a/lib/fields/date.js b/lib/fields/date.js index be5782e..b68617d 100644 --- a/lib/fields/date.js +++ b/lib/fields/date.js @@ -1,9 +1,11 @@ // Generated by CoffeeScript 1.6.2 (function() { - var $, formatBackDate, formatDate, formatForwardDate, formatForwardSlash, restrictDate; + var $, formatBackDate, formatDate, formatForwardDate, formatForwardSlash, hasTextSelected, restrictDate; $ = jQuery; + hasTextSelected = $.formance.fn.hasTextSelected; + restrictDate = function(e) { var $target, digit, value; @@ -73,12 +75,12 @@ parse_day = /^(\d)$/; parse_month = /^(\d{2})\s\/\s(\d)$/; if (parse_day.test(val) && val !== '0') { - $target.val("0" + val + " / "); + return $target.val("0" + val + " / "); } else if (parse_month.test(val)) { _ref = val.match(parse_month), date = _ref[0], day = _ref[1], month = _ref[2]; - } - if (month !== '0') { - return $target.val("" + day + " / 0" + month + " / "); + if (month !== '0') { + return $target.val("" + day + " / 0" + month + " / "); + } } }; @@ -105,32 +107,38 @@ } }; - $.formance.date = { - format: function() { - this.formatters('restrictNumeric'); - this.on('keypress', restrictDate); - this.on('keypress', formatDate); - this.on('keypress', formatForwardSlash); - this.on('keypress', formatForwardDate); - this.on('keydown', formatBackDate); - this; - return { - validate: function() { - var regex, val; - - val = $(this).val(); - if (val == null) { - return false; - } - val = val.replace(/[\s|\-]/g, ''); - if (!/^[a-zA-Z\d]+$/.test()) { - return false; - } - regex = /^(\d{4})[\s|\-]*?(\d{3})[\s|\-]*?(\d{3})[\s|\-]*?([A-Za-z]{2})$/; - return regest.test(val); - } - }; + $.formance.fn.formatDate = function() { + this.formance('restrictNumeric'); + this.on('keypress', restrictDate); + this.on('keypress', formatDate); + this.on('keypress', formatForwardSlash); + this.on('keypress', formatForwardDate); + this.on('keydown', formatBackDate); + return this; + }; + + $.formance.validateDate = function(day, month, year) { + if ((day == null) || isNaN(day) || (month == null) || isNaN(month) || (year == null) || isNaN(year)) { + return false; } + if (!(((0 < day && day <= 31)) && ((0 < month && month <= 12)) && ((1000 < year && year <= 10000)))) { + return false; + } + return true; + }; + + $.formance.dateVal = function(dateString) { + var day, month, year, _ref; + + _ref = dateString != null ? dateString.replace(/\s/g, '').split('/', 3) : [NaN, NaN, NaN], day = _ref[0], month = _ref[1], year = _ref[2]; + day = parseInt(day, 10); + month = parseInt(month, 10); + year = parseInt(year, 10); + return { + day: day, + month: month, + year: year + }; }; }).call(this); diff --git a/lib/fields/number.js b/lib/fields/number.js index ebae683..cabb0ca 100644 --- a/lib/fields/number.js +++ b/lib/fields/number.js @@ -4,17 +4,13 @@ $ = jQuery; - $.fn.formance.number = { - format: function() { - this.formatters('restrictNumeric'); - return this; - }, - validate: function() { - var val; + $.formance.fn.formatNumber = function() { + this.formance('restrictNumeric'); + return this; + }; - val = $(this).val(); - return /^\d+$/.test(val); - } + $.formance.validateNumber = function(val) { + return /^\d+$/.test(val); }; }).call(this); diff --git a/lib/fields/ontario_photo_health_card_number.js b/lib/fields/ontario_photo_health_card_number.js index e362864..34ce688 100644 --- a/lib/fields/ontario_photo_health_card_number.js +++ b/lib/fields/ontario_photo_health_card_number.js @@ -1,9 +1,11 @@ // Generated by CoffeeScript 1.6.2 (function() { - var $, formatBackOntarioPhotoHealthCardNumber, formatOntarioPhotoHealthCardNumber, formatPasteOntarioPhotoHealthCardNumber, restrictOntarioPhotoHealthCardNumber; + var $, formatBackOntarioPhotoHealthCardNumber, formatOntarioPhotoHealthCardNumber, formatPasteOntarioPhotoHealthCardNumber, hasTextSelected, restrictOntarioPhotoHealthCardNumber; $ = jQuery; + hasTextSelected = $.formance.fn.hasTextSelected; + restrictOntarioPhotoHealthCardNumber = function(e) { var $target, char, value; @@ -80,29 +82,27 @@ }); }; - $.fn.formance.ontarioPhotoHealthCardNumber = { - format: function() { - this.formatters('restrictAlphaNumeric'); - this.on('keypress', restrictOntarioPhotoHealthCardNumber); - this.on('keypress', formatOntarioPhotoHealthCardNumber); - this.on('keydown', formatBackOntarioPhotoHealthCardNumber); - this.on('paste', formatPasteOntarioPhotoHealthCardNumber); - return this; - }, - validate: function() { - var regex, val; + $.formance.fn.formatOntarioPhotoHealthCardNumber = function() { + this.formance('restrictAlphaNumeric'); + this.on('keypress', restrictOntarioPhotoHealthCardNumber); + this.on('keypress', formatOntarioPhotoHealthCardNumber); + this.on('keydown', formatBackOntarioPhotoHealthCardNumber); + this.on('paste', formatPasteOntarioPhotoHealthCardNumber); + return this; + }; + + $.formance.validateOntarioPhotoHealthCardNumber = function(val) { + var regex; - val = $(this).val(); - if (val == null) { - return false; - } - val = val.replace(/[\s|\-]/g, ''); - if (!/^[a-zA-Z\d]+$/.test()) { - return false; - } - regex = /^(\d{4})[\s|\-]*?(\d{3})[\s|\-]*?(\d{3})[\s|\-]*?([A-Za-z]{2})$/; - return regest.test(val); + if (val == null) { + return false; + } + val = val.replace(/[\s|\-]/g, ''); + if (!/^[a-zA-Z\d]+$/.test()) { + return false; } + regex = /^(\d{4})[\s|\-]*?(\d{3})[\s|\-]*?(\d{3})[\s|\-]*?([A-Za-z]{2})$/; + return regex.test(val); }; }).call(this); diff --git a/lib/fields/phone_number.js b/lib/fields/phone_number.js index 947a980..f2067fe 100644 --- a/lib/fields/phone_number.js +++ b/lib/fields/phone_number.js @@ -1,9 +1,11 @@ // Generated by CoffeeScript 1.6.2 (function() { - var $, formatBackPhoneNumber, formatPastePhoneNumber, formatPhoneNumber, reFormatPhoneNumber, restrictPhoneNumber; + var $, formatBackPhoneNumber, formatPastePhoneNumber, formatPhoneNumber, hasTextSelected, reFormatPhoneNumber, restrictPhoneNumber; $ = jQuery; + hasTextSelected = $.formance.fn.hasTextSelected; + reFormatPhoneNumber = function(phoneNumberString) { var areaCode, first3, last4, phoneNumber, text, _ref; @@ -98,25 +100,24 @@ }); }; - $.fn.formance.phoneNumber = { - format: function() { - this.formatters('restrictNumeric'); - this.on('keypress', restrictPhoneNumber); - this.on('keypress', formatPhoneNumber); - this.on('keydown', formatBackPhoneNumber); - this.on('paste', formatPastePhoneNumber); - return this; - }, - validate: function() { - var val; - - val = $(this).val(); - val = val.replace(/\(|\)|\s+|-/g, ''); - if (!/^\d+$/.test(val)) { - return false; - } - return val.replace(/\D/g, '').length === 10; + $.formance.fn.formatPhoneNumber = function() { + this.formance('restrictNumeric'); + this.on('keypress', restrictPhoneNumber); + this.on('keypress', formatPhoneNumber); + this.on('keydown', formatBackPhoneNumber); + this.on('paste', formatPastePhoneNumber); + return this; + }; + + $.formance.validatePhoneNumber = function(val) { + if (val == null) { + return false; + } + val = val.replace(/\(|\)|\s+|-/g, ''); + if (!/^\d+$/.test(val)) { + return false; } + return val.replace(/\D/g, '').length === 10; }; }).call(this); diff --git a/lib/fields/postal_code.js b/lib/fields/postal_code.js index 37c3d95..10f3adc 100644 --- a/lib/fields/postal_code.js +++ b/lib/fields/postal_code.js @@ -1,9 +1,11 @@ // Generated by CoffeeScript 1.6.2 (function() { - var $, formatBackPostalCode, formatPastePostalCode, formatPostalCode, restrictPostalCode; + var $, formatBackPostalCode, formatPastePostalCode, formatPostalCode, hasTextSelected, restrictPostalCode; $ = jQuery; + hasTextSelected = $.formance.fn.hasTextSelected; + restrictPostalCode = function(e) { var $target, char, value; @@ -98,29 +100,25 @@ }); }; - $.fn.formance.postalCode = { - format: function() { - this.formatters('restrictAlphaNumeric'); - this.on('keypress', restrictOntarioPhotoHealthCardNumber); - this.on('keypress', formatOntarioPhotoHealthCardNumber); - this.on('keydown', formatBackOntarioPhotoHealthCardNumber); - this.on('paste', formatPasteOntarioPhotoHealthCardNumber); - return this; - }, - validate: function() { - var val; + $.formance.fn.formatPostalCode = function() { + this.formance('restrictAlphaNumeric'); + this.on('keypress', restrictPostalCode); + this.on('keypress', formatPostalCode); + this.on('keydown', formatBackPostalCode); + this.on('paste', formatPastePostalCode); + return this; + }; - val = $(this).val(); - if (val == null) { - return false; - } - val = val.replace(/\s+/g, ''); - if (!/^[a-zA-Z\d]+$/.test(val)) { - return false; - } - val = val.replace(/[^a-zA-Z\d]/g, ''); - return /^[ABCEFGHJKLMNPRSTVXY][0-9][ABCEFGHJKLMNPRSTVWXYZ]\s?[0-9][ABCEFGHJKLMNPRSTVWXYZ][0-9]$/.test(val.toUpperCase()); + $.formance.validatePostalCode = function(val) { + if (val == null) { + return false; + } + val = val.replace(/\s+/g, ''); + if (!/^[a-zA-Z\d]+$/.test(val)) { + return false; } + val = val.replace(/[^a-zA-Z\d]/g, ''); + return /^[ABCEFGHJKLMNPRSTVXY][0-9][ABCEFGHJKLMNPRSTVWXYZ]\s?[0-9][ABCEFGHJKLMNPRSTVWXYZ][0-9]$/.test(val.toUpperCase()); }; }).call(this); diff --git a/lib/jquery.formance.js b/lib/jquery.formance.js index d445207..43dfdc2 100644 --- a/lib/jquery.formance.js +++ b/lib/jquery.formance.js @@ -1,14 +1,28 @@ // Generated by CoffeeScript 1.6.2 (function() { - var $, hasTextSelected, restrictAlphaNumeric, restrictNumeric; + var $, hasTextSelected, restrictAlphaNumeric, restrictNumeric, + __slice = [].slice; $ = jQuery; - $.fn.formance = {}; + $.formance = {}; + + $.formance.fn = {}; + + $.fn.formance = function() { + var args, method; + + method = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : []; + return $.formance.fn[method].apply(this, args); + }; restrictNumeric = function(e) { - var input; + var $target, input; + console.log('restrictNumeric triggered'); + $target = $(e.target); + console.log('"' + $target.val() + '"'); + return true; if (e.metaKey || e.ctrlKey) { return true; } @@ -22,12 +36,17 @@ return true; } input = String.fromCharCode(e.which); + console.log(!!/[\d\s]/.test(input)); return !!/[\d\s]/.test(input); }; restrictAlphaNumeric = function(e) { - var input; + var $target, input; + console.log('restrictAlphaNumeric triggered'); + $target = $(e.target); + console.log('"' + $target.val() + '"'); + return true; if (e.metaKey || e.ctrlKey) { return true; } @@ -41,6 +60,7 @@ return true; } input = String.fromCharCode(e.which); + console.log(!!/[\d\sA-Za-z]/.test(input)); return !!/[\d\sA-Za-z]/.test(input); }; @@ -56,18 +76,34 @@ return false; }; - $.fn.formance.restrictNumeric = function() { + $.formance.fn.restrictNumeric = function() { + console.log('Setting up keypress listener for restrictNumeric'); this.on('keypress', restrictNumeric); return this; }; - $.fn.formance.restrictAlphaNumeric = function() { + $.formance.fn.restrictAlphaNumeric = function() { + console.log('Setting up keypress listener for restrictAlphaNumeric'); this.on('keypress', restrictAlphaNumeric); return this; }; - $.fn.formance.hasTextSelected = hasTextSelected; + $.formance.fn.hasTextSelected = hasTextSelected; + + require('./fields/credit_card_cvc'); + + require('./fields/credit_card_expiry'); + + require('./fields/credit_card_number'); + + require('./fields/date'); + + require('./fields/number'); + + require('./fields/ontario_photo_health_card_number'); + + require('./fields/phone_number'); - require('./fields/*'); + require('./fields/postal_code'); }).call(this); diff --git a/lib/jquery.formatters.js b/lib/jquery.formatters.js deleted file mode 100644 index 70f8555..0000000 --- a/lib/jquery.formatters.js +++ /dev/null @@ -1,427 +0,0 @@ -// Generated by CoffeeScript 1.6.2 -(function() { - var $, formatBackDate, formatBackPhoneNumber, formatBackPostalCode, formatDate, formatForwardDate, formatForwardSlash, formatPastePhoneNumber, formatPastePostalCode, formatPhoneNumber, formatPostalCode, hasTextSelected, reFormatPhoneNumber, restrictAlphaNumeric, restrictDate, restrictNumeric, restrictPhoneNumber, restrictPostalCode, - __slice = [].slice; - - require('./jquery.payment'); - - $ = jQuery; - - $.formatters = {}; - - $.formatters.fn = {}; - - $.fn.formatters = function() { - var args, method; - - method = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : []; - return $.formatters.fn[method].apply(this, args); - }; - - hasTextSelected = function($target) { - var _ref; - - if (($target.prop('selectionStart') != null) && $target.prop('selectionStart') !== $target.prop('selectionEnd')) { - return true; - } - if (typeof document !== "undefined" && document !== null ? (_ref = document.selection) != null ? typeof _ref.createRange === "function" ? _ref.createRange().text : void 0 : void 0 : void 0) { - return true; - } - return false; - }; - - restrictNumeric = function(e) { - var input; - - if (e.metaKey || e.ctrlKey) { - return true; - } - if (e.which === 32) { - return false; - } - if (e.which === 0) { - return true; - } - if (e.which < 33) { - return true; - } - input = String.fromCharCode(e.which); - return !!/[\d\s]/.test(input); - }; - - restrictAlphaNumeric = function(e) { - var input; - - if (e.metaKey || e.ctrlKey) { - return true; - } - if (e.which === 32) { - return false; - } - if (e.which === 0) { - return true; - } - if (e.which < 33) { - return true; - } - input = String.fromCharCode(e.which); - return !!/[\d\sA-Za-z]/.test(input); - }; - - restrictDate = function(e) { - var $target, digit, value; - - $target = $(e.currentTarget); - digit = String.fromCharCode(e.which); - if (!/^\d+$/.test(digit)) { - return; - } - if (hasTextSelected($target)) { - return; - } - value = $target.val() + digit; - value = value.replace(/\D/g, ''); - if (value.length > 8) { - return false; - } - }; - - formatDate = function(e) { - var $target, digit, old_val, val; - - digit = String.fromCharCode(e.which); - if (!/^\d+$/.test(digit)) { - return; - } - $target = $(e.currentTarget); - old_val = $target.val(); - val = old_val + digit; - if (/^\d$/.test(val) && (digit !== '0' && digit !== '1' && digit !== '2' && digit !== '3')) { - e.preventDefault(); - return $target.val("0" + val + " / "); - } else if (/^\d{2}$/.test(val)) { - e.preventDefault(); - return $target.val("" + val + " / "); - } else if (/^\d{2}\s\/\s\d$/.test(val) && (digit !== '0' && digit !== '1')) { - e.preventDefault(); - return $target.val("" + old_val + "0" + digit + " / "); - } else if (/^\d{2}\s\/\s\d{2}$/.test(val)) { - e.preventDefault(); - return $target.val("" + val + " / "); - } - }; - - formatForwardDate = function(e) { - var $target, digit, val; - - digit = String.fromCharCode(e.which); - if (!/^\d+$/.test(digit)) { - return; - } - $target = $(e.currentTarget); - val = $target.val(); - if (/^\d{2}$/.test(val) || /^\d{2}\s\/\s\d{2}$/.test(val)) { - return $target.val("" + val + " / "); - } - }; - - formatForwardSlash = function(e) { - var $target, date, day, month, parse_day, parse_month, slash, val, _ref; - - slash = String.fromCharCode(e.which); - if (slash !== '/') { - return; - } - $target = $(e.currentTarget); - val = $target.val(); - parse_day = /^(\d)$/; - parse_month = /^(\d{2})\s\/\s(\d)$/; - if (parse_day.test(val) && val !== '0') { - return $target.val("0" + val + " / "); - } else if (parse_month.test(val)) { - _ref = val.match(parse_month), date = _ref[0], day = _ref[1], month = _ref[2]; - if (month !== '0') { - return $target.val("" + day + " / 0" + month + " / "); - } - } - }; - - formatBackDate = function(e) { - var $target, value; - - if (e.meta) { - return; - } - $target = $(e.currentTarget); - value = $target.val(); - if (e.which !== 8) { - return; - } - if (($target.prop('selectionStart') != null) && $target.prop('selectionStart') !== value.length) { - return; - } - if (/\d(\s|\/)+$/.test(value)) { - e.preventDefault(); - return $target.val(value.replace(/\d(\s|\/)*$/, '')); - } else if (/\s\/\s?\d?$/.test(value)) { - e.preventDefault(); - return $target.val(value.replace(/\s\/\s?\d?$/, '')); - } - }; - - reFormatPhoneNumber = function(phone_number_string) { - var area_code, first3, last4, phone_number, text, _ref; - - _ref = phone_number_string.replace(/\D/g, '').match(/^(\d{0,3})?(\d{0,3})?(\d{0,4})?$/), phone_number = _ref[0], area_code = _ref[1], first3 = _ref[2], last4 = _ref[3]; - text = ''; - if (area_code != null) { - text += "(" + area_code; - } - if ((area_code != null ? area_code.length : void 0) === 3) { - text += ") "; - } - if (first3 != null) { - text += "" + first3; - } - if ((first3 != null ? first3.length : void 0) === 3) { - text += " - "; - } - if (last4 != null) { - text += "" + last4; - } - return text; - }; - - restrictPhoneNumber = function(e) { - var $target, digit, value; - - $target = $(e.currentTarget); - digit = String.fromCharCode(e.which); - if (!/^\d+$/.test(digit)) { - return; - } - if (hasTextSelected($target)) { - return; - } - value = $target.val() + digit; - value = value.replace(/\D/g, ''); - if (value.length > 10) { - return false; - } - }; - - formatPhoneNumber = function(e) { - var $target, digit, text, val; - - digit = String.fromCharCode(e.which); - if (!/^\d+$/.test(digit)) { - return; - } - $target = $(e.currentTarget); - val = $target.val() + digit; - text = reFormatPhoneNumber(val); - e.preventDefault(); - return $target.val(text); - }; - - formatBackPhoneNumber = function(e) { - var $target, value; - - if (e.meta) { - return; - } - $target = $(e.currentTarget); - value = $target.val(); - if (e.which !== 8) { - return; - } - if (($target.prop('selectionStart') != null) && $target.prop('selectionStart') !== value.length) { - return; - } - if (/\(\d$/.test(value)) { - e.preventDefault(); - return $target.val(''); - } else if (/\d\)(\s)+$/.test(value)) { - e.preventDefault(); - return $target.val(value.replace(/\d\)(\s)*$/, '')); - } else if (/\d(\s|\-)+$/.test(value)) { - e.preventDefault(); - return $target.val(value.replace(/\d(\s|\-)+$/, '')); - } - }; - - formatPastePhoneNumber = function(e) { - var _this = this; - - return setTimeout(function() { - var $target, text, val; - - $target = $(e.currentTarget); - val = $target.val(); - text = reFormatPhoneNumber(val); - return $target.val(text); - }); - }; - - restrictPostalCode = function(e) { - var $target, char, value; - - $target = $(e.currentTarget); - char = String.fromCharCode(e.which); - if (!/^[a-zA-Z\d]+$/.test(char)) { - return; - } - if (hasTextSelected($target)) { - return; - } - value = $target.val() + char; - value = value.replace(/[^a-zA-Z\d]/g, ''); - if (value.length > 6) { - return false; - } - }; - - formatPostalCode = function(e) { - var $target, char, old_val, val; - - char = String.fromCharCode(e.which); - if (!/^[a-zA-Z\d]+$/.test(char)) { - return; - } - $target = $(e.currentTarget); - old_val = $target.val(); - val = old_val + char.toUpperCase(); - if (old_val === '') { - e.preventDefault(); - if (/^[ABCEFGHJKLMNPRSTVXY]$/.test(val)) { - return $target.val(val); - } - } else if (/^[ABCEFGHJKLMNPRSTVXY]$/.test(old_val)) { - e.preventDefault(); - if (/^[ABCEFGHJKLMNPRSTVXY][0-9]$/.test(val)) { - return $target.val(val); - } - } else if (/^[ABCEFGHJKLMNPRSTVXY][0-9]$/.test(old_val)) { - e.preventDefault(); - if (/^[ABCEFGHJKLMNPRSTVXY][0-9][ABCEFGHJKLMNPRSTVWXYZ]$/.test(val)) { - return $target.val("" + val + " "); - } - } else if (/^[ABCEFGHJKLMNPRSTVXY][0-9][ABCEFGHJKLMNPRSTVWXYZ]\s$/.test(old_val)) { - e.preventDefault(); - if (/^[ABCEFGHJKLMNPRSTVXY][0-9][ABCEFGHJKLMNPRSTVWXYZ]\s?[0-9]$/.test(val)) { - return $target.val(val); - } - } else if (/^[ABCEFGHJKLMNPRSTVXY][0-9][ABCEFGHJKLMNPRSTVWXYZ]\s?[0-9]$/.test(old_val)) { - e.preventDefault(); - if (/^[ABCEFGHJKLMNPRSTVXY][0-9][ABCEFGHJKLMNPRSTVWXYZ]\s?[0-9][ABCEFGHJKLMNPRSTVWXYZ]$/.test(val)) { - return $target.val(val); - } - } else if (/^[ABCEFGHJKLMNPRSTVXY][0-9][ABCEFGHJKLMNPRSTVWXYZ]\s?[0-9][ABCEFGHJKLMNPRSTVWXYZ]$/.test(old_val)) { - e.preventDefault(); - if (/^[ABCEFGHJKLMNPRSTVXY][0-9][ABCEFGHJKLMNPRSTVWXYZ]\s?[0-9][ABCEFGHJKLMNPRSTVWXYZ][0-9]$/.test(val)) { - return $target.val(val); - } - } - }; - - formatBackPostalCode = function(e) { - var $target, value; - - if (e.meta) { - return; - } - $target = $(e.currentTarget); - value = $target.val(); - if (e.which !== 8) { - return; - } - if (($target.prop('selectionStart') != null) && $target.prop('selectionStart') !== value.length) { - return; - } - if (/[ABCEFGHJKLMNPRSTVWXYZ](\s)+$/.test(value)) { - e.preventDefault(); - return $target.val(value.replace(/[ABCEFGHJKLMNPRSTVWXYZ](\s)*$/, '')); - } - }; - - formatPastePostalCode = function(e) { - var _this = this; - - return setTimeout(function() { - var $target, first_part, full, second_part, val, _ref; - - $target = $(e.currentTarget); - val = $target.val(); - _ref = val.match(/^([ABCEFGHJKLMNPRSTVXY][0-9][ABCEFGHJKLMNPRSTVWXYZ])\s?([0-9][ABCEFGHJKLMNPRSTVWXYZ][0-9])$/), full = _ref[0], first_part = _ref[1], second_part = _ref[2]; - return $target.val("" + first_part + " " + second_part); - }); - }; - - $.formatters.fn.formatCardNumber = function() { - this.payment('formatCardNumber'); - return this; - }; - - $.formatters.fn.formatCardExpiry = function() { - this.payment('formatCardExpiry'); - return this; - }; - - $.formatters.fn.formatCardCVC = function() { - this.payment('formatCardCVC'); - return this; - }; - - $.formatters.fn.formatDate = function() { - this.formatters('restrictNumeric'); - this.on('keypress', restrictDate); - this.on('keypress', formatDate); - this.on('keypress', formatForwardSlash); - this.on('keypress', formatForwardDate); - this.on('keydown', formatBackDate); - return this; - }; - - $.formatters.fn.formatNumber = function() { - this.formatters('restrictNumeric'); - return this; - }; - - $.formatters.fn.formatOntarioHealthCardNumber = function() { - this.formatters('restrictAlphaNumeric'); - this.on('keypress', restrictOntarioPhotoHealthCardNumber); - this.on('keypress', formatOntarioPhotoHealthCardNumber); - this.on('keydown', formatBackOntarioPhotoHealthCardNumber); - this.on('paste', formatPasteOntarioPhotoHealthCardNumber); - return this; - }; - - $.formatters.fn.formatPhoneNumber = function() { - this.formatters('restrictNumeric'); - this.on('keypress', restrictPhoneNumber); - this.on('keypress', formatPhoneNumber); - this.on('keydown', formatBackPhoneNumber); - this.on('paste', formatPastePhoneNumber); - return this; - }; - - $.formatters.fn.formatPostalCode = function() { - this.formatters('restrictAlphaNumeric'); - this.on('keypress', restrictPostalCode); - this.on('keypress', formatPostalCode); - this.on('keydown', formatBackPostalCode); - this.on('paste', formatPastePostalCode); - return this; - }; - - $.formatters.fn.restrictNumeric = function() { - this.on('keypress', restrictNumeric); - return this; - }; - - $.formatters.fn.restrictAlphaNumeric = function() { - this.on('keypress', restrictAlphaNumeric); - return this; - }; - -}).call(this); diff --git a/lib/jquery.payment.js b/lib/jquery.payment.js deleted file mode 100644 index d25f7a1..0000000 --- a/lib/jquery.payment.js +++ /dev/null @@ -1,522 +0,0 @@ -// Generated by CoffeeScript 1.6.2 -(function() { - var $, cardFromNumber, cardFromType, cards, defaultFormat, formatBackCardNumber, formatBackExpiry, formatCardNumber, formatExpiry, formatForwardExpiry, formatForwardSlash, hasTextSelected, luhnCheck, reFormatCardNumber, restrictCVC, restrictCardNumber, restrictExpiry, restrictNumeric, setCardType, - __slice = [].slice, - __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; }, - _this = this; - - $ = jQuery; - - $.payment = {}; - - $.payment.fn = {}; - - $.fn.payment = function() { - var args, method; - - method = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : []; - return $.payment.fn[method].apply(this, args); - }; - - defaultFormat = /(\d{1,4})/g; - - cards = [ - { - type: 'maestro', - pattern: /^(5018|5020|5038|6304|6759|676[1-3])/, - format: defaultFormat, - length: [12, 13, 14, 15, 16, 17, 18, 19], - cvcLength: [3], - luhn: true - }, { - type: 'dinersclub', - pattern: /^(36|38|30[0-5])/, - format: defaultFormat, - length: [14], - cvcLength: [3], - luhn: true - }, { - type: 'laser', - pattern: /^(6706|6771|6709)/, - format: defaultFormat, - length: [16, 17, 18, 19], - cvcLength: [3], - luhn: true - }, { - type: 'jcb', - pattern: /^35/, - format: defaultFormat, - length: [16], - cvcLength: [3], - luhn: true - }, { - type: 'unionpay', - pattern: /^62/, - format: defaultFormat, - length: [16, 17, 18, 19], - cvcLength: [3], - luhn: false - }, { - type: 'discover', - pattern: /^(6011|65|64[4-9]|622)/, - format: defaultFormat, - length: [16], - cvcLength: [3], - luhn: true - }, { - type: 'mastercard', - pattern: /^5[1-5]/, - format: defaultFormat, - length: [16], - cvcLength: [3], - luhn: true - }, { - type: 'amex', - pattern: /^3[47]/, - format: /(\d{1,4})(\d{1,6})?(\d{1,5})?/, - length: [15], - cvcLength: [3, 4], - luhn: true - }, { - type: 'visa', - pattern: /^4/, - format: defaultFormat, - length: [13, 14, 15, 16], - cvcLength: [3], - luhn: true - } - ]; - - cardFromNumber = function(num) { - var card, _i, _len; - - num = (num + '').replace(/\D/g, ''); - for (_i = 0, _len = cards.length; _i < _len; _i++) { - card = cards[_i]; - if (card.pattern.test(num)) { - return card; - } - } - }; - - cardFromType = function(type) { - var card, _i, _len; - - for (_i = 0, _len = cards.length; _i < _len; _i++) { - card = cards[_i]; - if (card.type === type) { - return card; - } - } - }; - - luhnCheck = function(num) { - var digit, digits, odd, sum, _i, _len; - - odd = true; - sum = 0; - digits = (num + '').split('').reverse(); - for (_i = 0, _len = digits.length; _i < _len; _i++) { - digit = digits[_i]; - digit = parseInt(digit, 10); - if ((odd = !odd)) { - digit *= 2; - } - if (digit > 9) { - digit -= 9; - } - sum += digit; - } - return sum % 10 === 0; - }; - - hasTextSelected = function($target) { - var _ref; - - if (($target.prop('selectionStart') != null) && $target.prop('selectionStart') !== $target.prop('selectionEnd')) { - return true; - } - if (typeof document !== "undefined" && document !== null ? (_ref = document.selection) != null ? typeof _ref.createRange === "function" ? _ref.createRange().text : void 0 : void 0 : void 0) { - return true; - } - return false; - }; - - reFormatCardNumber = function(e) { - var _this = this; - - return setTimeout(function() { - var $target, value; - - $target = $(e.currentTarget); - value = $target.val(); - value = $.payment.formatCardNumber(value); - return $target.val(value); - }); - }; - - formatCardNumber = function(e) { - var $target, card, digit, length, re, upperLength, value; - - digit = String.fromCharCode(e.which); - if (!/^\d+$/.test(digit)) { - return; - } - $target = $(e.currentTarget); - value = $target.val(); - card = cardFromNumber(value + digit); - length = (value.replace(/\D/g, '') + digit).length; - upperLength = 16; - if (card) { - upperLength = card.length[card.length.length - 1]; - } - if (length >= upperLength) { - return; - } - if (($target.prop('selectionStart') != null) && $target.prop('selectionStart') !== value.length) { - return; - } - if (card && card.type === 'amex') { - re = /^(\d{4}|\d{4}\s\d{6})$/; - } else { - re = /(?:^|\s)(\d{4})$/; - } - if (re.test(value)) { - e.preventDefault(); - return $target.val(value + ' ' + digit); - } else if (re.test(value + digit)) { - e.preventDefault(); - return $target.val(value + digit + ' '); - } - }; - - formatBackCardNumber = function(e) { - var $target, value; - - $target = $(e.currentTarget); - value = $target.val(); - if (e.meta) { - return; - } - if (e.which !== 8) { - return; - } - if (($target.prop('selectionStart') != null) && $target.prop('selectionStart') !== value.length) { - return; - } - if (/\d\s$/.test(value)) { - e.preventDefault(); - return $target.val(value.replace(/\d\s$/, '')); - } else if (/\s\d?$/.test(value)) { - e.preventDefault(); - return $target.val(value.replace(/\s\d?$/, '')); - } - }; - - formatExpiry = function(e) { - var $target, digit, val; - - digit = String.fromCharCode(e.which); - if (!/^\d+$/.test(digit)) { - return; - } - $target = $(e.currentTarget); - val = $target.val() + digit; - if (/^\d$/.test(val) && (val !== '0' && val !== '1')) { - e.preventDefault(); - return $target.val("0" + val + " / "); - } else if (/^\d\d$/.test(val)) { - e.preventDefault(); - return $target.val("" + val + " / "); - } - }; - - formatForwardExpiry = function(e) { - var $target, digit, val; - - digit = String.fromCharCode(e.which); - if (!/^\d+$/.test(digit)) { - return; - } - $target = $(e.currentTarget); - val = $target.val(); - if (/^\d\d$/.test(val)) { - return $target.val("" + val + " / "); - } - }; - - formatForwardSlash = function(e) { - var $target, slash, val; - - slash = String.fromCharCode(e.which); - if (slash !== '/') { - return; - } - $target = $(e.currentTarget); - val = $target.val(); - if (/^\d$/.test(val) && val !== '0') { - return $target.val("0" + val + " / "); - } - }; - - formatBackExpiry = function(e) { - var $target, value; - - if (e.meta) { - return; - } - $target = $(e.currentTarget); - value = $target.val(); - if (e.which !== 8) { - return; - } - if (($target.prop('selectionStart') != null) && $target.prop('selectionStart') !== value.length) { - return; - } - if (/\d(\s|\/)+$/.test(value)) { - e.preventDefault(); - return $target.val(value.replace(/\d(\s|\/)*$/, '')); - } else if (/\s\/\s?\d?$/.test(value)) { - e.preventDefault(); - return $target.val(value.replace(/\s\/\s?\d?$/, '')); - } - }; - - restrictNumeric = function(e) { - var input; - - if (e.metaKey || e.ctrlKey) { - return true; - } - if (e.which === 32) { - return false; - } - if (e.which === 0) { - return true; - } - if (e.which < 33) { - return true; - } - input = String.fromCharCode(e.which); - return !!/[\d\s]/.test(input); - }; - - restrictCardNumber = function(e) { - var $target, card, digit, value; - - $target = $(e.currentTarget); - digit = String.fromCharCode(e.which); - if (!/^\d+$/.test(digit)) { - return; - } - if (hasTextSelected($target)) { - return; - } - value = ($target.val() + digit).replace(/\D/g, ''); - card = cardFromNumber(value); - if (card) { - return value.length <= card.length[card.length.length - 1]; - } else { - return value.length <= 16; - } - }; - - restrictExpiry = function(e) { - var $target, digit, value; - - $target = $(e.currentTarget); - digit = String.fromCharCode(e.which); - if (!/^\d+$/.test(digit)) { - return; - } - if (hasTextSelected($target)) { - return; - } - value = $target.val() + digit; - value = value.replace(/\D/g, ''); - if (value.length > 6) { - return false; - } - }; - - restrictCVC = function(e) { - var $target, digit, val; - - $target = $(e.currentTarget); - digit = String.fromCharCode(e.which); - if (!/^\d+$/.test(digit)) { - return; - } - val = $target.val() + digit; - return val.length <= 4; - }; - - setCardType = function(e) { - var $target, allTypes, card, cardType, val; - - $target = $(e.currentTarget); - val = $target.val(); - cardType = $.payment.cardType(val) || 'unknown'; - if (!$target.hasClass(cardType)) { - allTypes = (function() { - var _i, _len, _results; - - _results = []; - for (_i = 0, _len = cards.length; _i < _len; _i++) { - card = cards[_i]; - _results.push(card.type); - } - return _results; - })(); - $target.removeClass('unknown'); - $target.removeClass(allTypes.join(' ')); - $target.addClass(cardType); - $target.toggleClass('identified', cardType !== 'unknown'); - return $target.trigger('payment.cardType', cardType); - } - }; - - $.payment.fn.formatCardCVC = function() { - this.payment('restrictNumeric'); - this.on('keypress', restrictCVC); - return this; - }; - - $.payment.fn.formatCardExpiry = function() { - this.payment('restrictNumeric'); - this.on('keypress', restrictExpiry); - this.on('keypress', formatExpiry); - this.on('keypress', formatForwardSlash); - this.on('keypress', formatForwardExpiry); - this.on('keydown', formatBackExpiry); - return this; - }; - - $.payment.fn.formatCardNumber = function() { - this.payment('restrictNumeric'); - this.on('keypress', restrictCardNumber); - this.on('keypress', formatCardNumber); - this.on('keydown', formatBackCardNumber); - this.on('keyup', setCardType); - this.on('paste', reFormatCardNumber); - return this; - }; - - $.payment.fn.restrictNumeric = function() { - this.on('keypress', restrictNumeric); - return this; - }; - - $.payment.fn.cardExpiryVal = function() { - return $.payment.cardExpiryVal($(this).val()); - }; - - $.payment.cardExpiryVal = function(value) { - var month, prefix, year, _ref; - - value = value.replace(/\s/g, ''); - _ref = value.split('/', 2), month = _ref[0], year = _ref[1]; - if ((year != null ? year.length : void 0) === 2 && /^\d+$/.test(year)) { - prefix = (new Date).getFullYear(); - prefix = prefix.toString().slice(0, 2); - year = prefix + year; - } - month = parseInt(month, 10); - year = parseInt(year, 10); - return { - month: month, - year: year - }; - }; - - $.payment.validateCardNumber = function(num) { - var card, _ref; - - num = (num + '').replace(/\s+|-/g, ''); - if (!/^\d+$/.test(num)) { - return false; - } - card = cardFromNumber(num); - if (!card) { - return false; - } - return (_ref = num.length, __indexOf.call(card.length, _ref) >= 0) && (card.luhn === false || luhnCheck(num)); - }; - - $.payment.validateCardExpiry = function(month, year) { - var currentTime, expiry, prefix, _ref; - - if (typeof month === 'object' && 'month' in month) { - _ref = month, month = _ref.month, year = _ref.year; - } - if (!(month && year)) { - return false; - } - month = $.trim(month); - year = $.trim(year); - if (!/^\d+$/.test(month)) { - return false; - } - if (!/^\d+$/.test(year)) { - return false; - } - if (!(parseInt(month, 10) <= 12)) { - return false; - } - if (year.length === 2) { - prefix = (new Date).getFullYear(); - prefix = prefix.toString().slice(0, 2); - year = prefix + year; - } - expiry = new Date(year, month); - currentTime = new Date; - expiry.setMonth(expiry.getMonth() - 1); - expiry.setMonth(expiry.getMonth() + 1, 1); - return expiry > currentTime; - }; - - $.payment.validateCardCVC = function(cvc, type) { - var _ref, _ref1; - - cvc = $.trim(cvc); - if (!/^\d+$/.test(cvc)) { - return false; - } - if (type) { - return _ref = cvc.length, __indexOf.call((_ref1 = cardFromType(type)) != null ? _ref1.cvcLength : void 0, _ref) >= 0; - } else { - return cvc.length >= 3 && cvc.length <= 4; - } - }; - - $.payment.cardType = function(num) { - var _ref; - - if (!num) { - return null; - } - return ((_ref = cardFromNumber(num)) != null ? _ref.type : void 0) || null; - }; - - $.payment.formatCardNumber = function(num) { - var card, groups, upperLength, _ref; - - card = cardFromNumber(num); - if (!card) { - return num; - } - upperLength = card.length[card.length.length - 1]; - num = num.replace(/\D/g, ''); - num = num.slice(0, +upperLength + 1 || 9e9); - if (card.format.global) { - return (_ref = num.match(card.format)) != null ? _ref.join(' ') : void 0; - } else { - groups = card.format.exec(num); - if (groups != null) { - groups.shift(); - } - return groups != null ? groups.join(' ') : void 0; - } - }; - -}).call(this); diff --git a/lib/jquery.validators.js b/lib/jquery.validators.js deleted file mode 100644 index 9f3b819..0000000 --- a/lib/jquery.validators.js +++ /dev/null @@ -1,105 +0,0 @@ -// Generated by CoffeeScript 1.6.2 -(function() { - var $, - __slice = [].slice; - - require('./jquery.payment'); - - $ = jQuery; - - $.validators = {}; - - $.validators.fn = {}; - - $.fn.validators = function() { - var args, method; - - method = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : []; - return $.validators.fn[method].apply(this, args); - }; - - $.validators.validateCardNumber = function(num) { - return $.payment.validateCardNumber(num); - }; - - $.validators.validateCardExpiry = function(expiry_string) { - var expiry; - - expiry = $.payment.cardExpiryVal(expiry_string); - return $.payment.validateCardExpiry(expiry.month, expiry.year); - }; - - $.validators.validateCardCVC = function(cvc, type) { - return $.payment.validateCardCVC(cvc, type); - }; - - $.validators.validateDate = function(day, month, year) { - if ((day == null) || isNaN(day) || (month == null) || isNaN(month) || (year == null) || isNaN(year)) { - return false; - } - if (!(((0 < day && day <= 31)) && ((0 < month && month <= 12)) && ((1000 < year && year <= 10000)))) { - return false; - } - return true; - }; - - $.validators.validateNumber = function(num) { - return /^\d+$/.test(num); - }; - - $.validators.validateOntarioPhotoHealthCardNumber = function(str) { - var regex, string; - - if (str == null) { - return false; - } - regex = /^(\d{4})[\s|\-]*?(\d{3})[\s|\-]*?(\d{3})[\s|\-]*?([A-Za-z]{2})$/; - string = str.replace(/[^a-zA-Z\d]/g, ''); - return regex.test(str); - }; - - $.validators.validatePhoneNumber = function(phone_string) { - phone_string = phone_string.replace(/\(|\)|\s+|-/g, ''); - if (!/^\d+$/.test(phone_string)) { - return false; - } - return phone_string.replace(/\D/g, '').length === 10; - }; - - $.validators.validatePostalCode = function(postal_code_string) { - if (postal_code_string == null) { - return false; - } - postal_code_string = postal_code_string.replace(/\s+/g, ''); - if (!/^[a-zA-Z\d]+$/.test(postal_code_string)) { - return false; - } - postal_code_string = postal_code_string.replace(/[^a-zA-Z\d]/g, ''); - return /^[ABCEFGHJKLMNPRSTVXY][0-9][ABCEFGHJKLMNPRSTVWXYZ]\s?[0-9][ABCEFGHJKLMNPRSTVWXYZ][0-9]$/.test(postal_code_string.toUpperCase()); - }; - - $.validators.fn.dateVal = function() { - return $.validators.dateVal($(this).val()); - }; - - $.validators.dateVal = function(date_string) { - var day, month, year, _ref; - - _ref = date_string != null ? date_string.replace(/\s/g, '').split('/', 3) : [NaN, NaN, NaN], day = _ref[0], month = _ref[1], year = _ref[2]; - day = parseInt(day, 10); - month = parseInt(month, 10); - year = parseInt(year, 10); - return { - day: day, - month: month, - year: year - }; - }; - - $.validators.fn.cardExpiryVal = function() { - return $validators.cardExpiryVal($(this).val()); - }; - - $.validators.cardExpiryVal = $.payment.cardExpiryVal; - -}).call(this); diff --git a/src/fields/credit_card_cvc.coffee b/src/fields/credit_card_cvc.coffee index 6eef5f9..9ab6754 100644 --- a/src/fields/credit_card_cvc.coffee +++ b/src/fields/credit_card_cvc.coffee @@ -2,16 +2,9 @@ require('./jquery.payment') $ = jQuery -$.fn.formance.creditCardCVC = +$.formance.fn.formatCreditCardCVC = -> + this.payment('formatCardCVC') + this - format: -> - this.payment('formatCardExpiry') - this - - validate: -> - val = $(this).val() - type = null # TODO credit card type, not passed in - return $.payment.validateCardCVC(val, type) - - parse: (expiryString) -> - $.payment.cardExpiryVal(expiryString) \ No newline at end of file +$.formance.validateCreditCardCVC = (val, type) -> + return $.payment.validateCardCVC(val, type) \ No newline at end of file diff --git a/src/fields/credit_card_expiry.coffee b/src/fields/credit_card_expiry.coffee index 5a98b8c..01a4005 100644 --- a/src/fields/credit_card_expiry.coffee +++ b/src/fields/credit_card_expiry.coffee @@ -2,16 +2,12 @@ require('./jquery.payment') $ = jQuery -$.fn.formance.creditCardExpiry = +$.formance.fn.formatCreditCardExpiry = -> + this.payment('formatCardExpiry') + this - format: -> - this.payment('formatCardExpiry') - this +$.formance.validateCreditCardExpiry = (month, year) -> + return $.payment.validateCardExpiry(month, year) - validate: -> - val = $(this).val() - expiry = @parse(val) - return $.payment.validateCardExpiry(expiry.month, expiry.year) - - parse: (expiryString) -> - $.payment.cardExpiryVal(expiryString) \ No newline at end of file +$.formance.creditCardExpiryVal = (expiryString) -> + $.payment.cardExpiryVal(expiryString) \ No newline at end of file diff --git a/src/fields/credit_card_number.coffee b/src/fields/credit_card_number.coffee index d50f704..9e5f88e 100644 --- a/src/fields/credit_card_number.coffee +++ b/src/fields/credit_card_number.coffee @@ -2,12 +2,12 @@ require('./jquery.payment') $ = jQuery -$.fn.formance.creditCardNumber = +$.formance.fn.formatCreditCardNumber = -> + this.payment('formatCardNumber') + this - format: -> - this.payment('formatCardNumber') - this +$.formance.validateCreditCardNumber = (num) -> + return $.payment.validateCardNumber(num) - validate: -> - val = $(this).val() - return $.payment.validateCardNumber(num) +$.formance.creditCardType = (num) -> + return $.payment.cardType(num) \ No newline at end of file diff --git a/src/fields/date.coffee b/src/fields/date.coffee index a4e834e..57fa522 100644 --- a/src/fields/date.coffee +++ b/src/fields/date.coffee @@ -1,4 +1,5 @@ $ = jQuery +hasTextSelected = $.formance.fn.hasTextSelected restrictDate = (e) -> $target = $(e.currentTarget) @@ -60,7 +61,7 @@ formatForwardSlash = (e) -> # handles when the user hits '/' else if parse_month.test(val) [date, day, month] = val.match(parse_month) if month isnt '0' - $target.val("#{day} / 0#{month} / ") + $target.val("#{day} / 0#{month} / ") formatBackDate = (e) -> # If shift+backspace is pressed @@ -84,33 +85,29 @@ formatBackDate = (e) -> e.preventDefault() $target.val(value.replace(/\s\/\s?\d?$/, '')) -$.fn.formance.date = - format: -> - @formatters('restrictNumeric') - @on('keypress', restrictDate) - @on('keypress', formatDate) - @on('keypress', formatForwardSlash) - @on('keypress', formatForwardDate) - @on('keydown', formatBackDate) - this +$.formance.fn.formatDate = -> + @.formance('restrictNumeric') + @on('keypress', restrictDate) + @on('keypress', formatDate) + @on('keypress', formatForwardSlash) + @on('keypress', formatForwardDate) + @on('keydown', formatBackDate) + this - validate: -> - val = $(this).val() - date = @parse(val) +$.formance.validateDate = (day, month, year) -> + return false if not day? or isNaN(day) or not month? or isNaN(month) or not year? or isNaN(year) + return false unless (0 < day <= 31) and (0 < month <= 12) and (1000 < year <= 10000) #can probably use better logic for 'valid' year + return true - return false if not date.day? or isNaN(date.day) or not date.month? or isNaN(date.month) or not date.year? or isNaN(date.year) - return false unless (0 < date.day <= 31) and (0 < date.month <= 12) and (1000 < date.year <= 10000) #can probably use better logic for 'valid' year - return true +$.formance.dateVal = (dateString) -> + [day, month, year] = if dateString? then dateString.replace(/\s/g, '').split('/', 3) else [NaN, NaN, NaN] - parse: (dateString) -> - [day, month, year] = if dateString? then dateString.replace(/\s/g, '').split('/', 3) else [NaN, NaN, NaN] + day = parseInt(day, 10) + month = parseInt(month, 10) + year = parseInt(year, 10) - day = parseInt(day, 10) - month = parseInt(month, 10) - year = parseInt(year, 10) - - return day: day, month: month, year: year + return day: day, month: month, year: year diff --git a/src/fields/number.coffee b/src/fields/number.coffee index 90acf3c..dc65acd 100644 --- a/src/fields/number.coffee +++ b/src/fields/number.coffee @@ -1,11 +1,8 @@ $ = jQuery -$.fn.formance.number = +$.formance.fn.formatNumber = -> + @.formance('restrictNumeric') + this - format: -> - @formatters('restrictNumeric') - this - - validate: -> - val = $(this).val() - return /^\d+$/.test(val) +$.formance.validateNumber = (val) -> + return /^\d+$/.test(val) \ No newline at end of file diff --git a/src/fields/ontario_photo_health_card_number.coffee b/src/fields/ontario_photo_health_card_number.coffee index 98c26be..69ce43b 100644 --- a/src/fields/ontario_photo_health_card_number.coffee +++ b/src/fields/ontario_photo_health_card_number.coffee @@ -1,5 +1,5 @@ $ = jQuery - +hasTextSelected = $.formance.fn.hasTextSelected restrictOntarioPhotoHealthCardNumber = (e) -> $target = $(e.currentTarget) @@ -64,23 +64,19 @@ formatPasteOntarioPhotoHealthCardNumber = (e) -> $target.val("#{first_four} - #{second_three} - #{third_three} - #{last_two}") -$.fn.formance.ontarioPhotoHealthCardNumber = - - format: -> - @formatters('restrictAlphaNumeric') - @on('keypress', restrictOntarioPhotoHealthCardNumber) - @on('keypress', formatOntarioPhotoHealthCardNumber) - @on('keydown', formatBackOntarioPhotoHealthCardNumber) - @on('paste', formatPasteOntarioPhotoHealthCardNumber) - this - - validate: -> - val = $(this).val() - - return false unless val? - val = val.replace(/[\s|\-]/g, '') - return false unless /^[a-zA-Z\d]+$/.test() +$.formance.fn.formatOntarioPhotoHealthCardNumber = -> + @.formance('restrictAlphaNumeric') + @on('keypress', restrictOntarioPhotoHealthCardNumber) + @on('keypress', formatOntarioPhotoHealthCardNumber) + @on('keydown', formatBackOntarioPhotoHealthCardNumber) + @on('paste', formatPasteOntarioPhotoHealthCardNumber) + this - regex = /^(\d{4})[\s|\-]*?(\d{3})[\s|\-]*?(\d{3})[\s|\-]*?([A-Za-z]{2})$/ - return regest.test(val) +$.formance.validateOntarioPhotoHealthCardNumber = (val) -> + return false unless val? + val = val.replace(/[\s|\-]/g, '') + return false unless /^[a-zA-Z\d]+$/.test() + regex = /^(\d{4})[\s|\-]*?(\d{3})[\s|\-]*?(\d{3})[\s|\-]*?([A-Za-z]{2})$/ + return regex.test(val) + diff --git a/src/fields/phone_number.coffee b/src/fields/phone_number.coffee index e63f166..99f94ca 100644 --- a/src/fields/phone_number.coffee +++ b/src/fields/phone_number.coffee @@ -1,4 +1,5 @@ $ = jQuery +hasTextSelected = $.formance.fn.hasTextSelected reFormatPhoneNumber = (phoneNumberString) -> [phoneNumber, areaCode, first3, last4] = phoneNumberString.replace(/\D/g, '').match(/^(\d{0,3})?(\d{0,3})?(\d{0,4})?$/) @@ -72,21 +73,18 @@ formatPastePhoneNumber = (e) -> $target.val(text) -$.fn.formance.phoneNumber = +$.formance.fn.formatPhoneNumber = -> + @.formance('restrictNumeric') + @on('keypress', restrictPhoneNumber) + @on('keypress', formatPhoneNumber) + @on('keydown', formatBackPhoneNumber) + @on('paste', formatPastePhoneNumber) + this - format: -> - @formatters('restrictNumeric') - @on('keypress', restrictPhoneNumber) - @on('keypress', formatPhoneNumber) - @on('keydown', formatBackPhoneNumber) - @on('paste', formatPastePhoneNumber) - this +$.formance.validatePhoneNumber = (val) -> + return false unless val? + val = val.replace(/\(|\)|\s+|-/g, '') + return false unless /^\d+$/.test(val) - validate: -> - val = $(this).val() - - val = val.replace(/\(|\)|\s+|-/g, '') - return false unless /^\d+$/.test(val) - - # [areaCode, first3, last4] = val.match(/\d+/g) - return val.replace(/\D/g, '').length is 10 # replaces all non digits [^0-9] with '' + # [areaCode, first3, last4] = val.match(/\d+/g) + return val.replace(/\D/g, '').length is 10 # replaces all non digits [^0-9] with '' \ No newline at end of file diff --git a/src/fields/postal_code.coffee b/src/fields/postal_code.coffee index 2147ad2..74cf188 100644 --- a/src/fields/postal_code.coffee +++ b/src/fields/postal_code.coffee @@ -1,4 +1,5 @@ $ = jQuery +hasTextSelected = $.formance.fn.hasTextSelected restrictPostalCode = (e) -> $target = $(e.currentTarget) @@ -75,28 +76,24 @@ formatPastePostalCode = (e) -> -$.fn.formance.postalCode = - format: -> - # $(this).formance.restrictAlphaNumeric() - @formatters('restrictAlphaNumeric') - @on('keypress', restrictOntarioPhotoHealthCardNumber) - @on('keypress', formatOntarioPhotoHealthCardNumber) - @on('keydown', formatBackOntarioPhotoHealthCardNumber) - @on('paste', formatPasteOntarioPhotoHealthCardNumber) - this +$.formance.fn.formatPostalCode = -> + @.formance('restrictAlphaNumeric') + @on('keypress', restrictPostalCode) + @on('keypress', formatPostalCode) + @on('keydown', formatBackPostalCode) + @on('paste', formatPastePostalCode) + this - validate: -> - val = $(this).val() +$.formance.validatePostalCode = (val) -> + return false unless val? + val = val.replace(/\s+/g, '') + return false unless /^[a-zA-Z\d]+$/.test(val) - return false unless val? - val = val.replace(/\s+/g, '') - return false unless /^[a-zA-Z\d]+$/.test(val) - - # http://stackoverflow.com/questions/1146202/canada-postal-code-validation - # apparently some letters are restricted - # - first letter can't be D,I,O,Q,U,W,Z - # - second letter can't be D,I,O,Q,U - # - third letter can't be D,I,O,Q,U - val = val.replace(/[^a-zA-Z\d]/g, '') #\W allows certain special characters - /^[ABCEFGHJKLMNPRSTVXY][0-9][ABCEFGHJKLMNPRSTVWXYZ]\s?[0-9][ABCEFGHJKLMNPRSTVWXYZ][0-9]$/.test(val.toUpperCase()) + # http://stackoverflow.com/questions/1146202/canada-postal-code-validation + # apparently some letters are restricted + # - first letter can't be D,I,O,Q,U,W,Z + # - second letter can't be D,I,O,Q,U + # - third letter can't be D,I,O,Q,U + val = val.replace(/[^a-zA-Z\d]/g, '') #\W allows certain special characters + /^[ABCEFGHJKLMNPRSTVXY][0-9][ABCEFGHJKLMNPRSTVWXYZ]\s?[0-9][ABCEFGHJKLMNPRSTVWXYZ][0-9]$/.test(val.toUpperCase()) diff --git a/src/jquery.formance.coffee b/src/jquery.formance.coffee index 2bc7b36..8c0291d 100644 --- a/src/jquery.formance.coffee +++ b/src/jquery.formance.coffee @@ -1,7 +1,16 @@ $ = jQuery -$.fn.formance = {} +$.formance = {} +$.formance.fn = {} +$.fn.formance = (method, args...) -> + $.formance.fn[method].apply(this, args) restrictNumeric = (e) -> + + console.log 'restrictNumeric triggered' + $target = $(e.target) + console.log '"' + $target.val() + '"' + return true + # Key event is for a browser shortcut return true if e.metaKey or e.ctrlKey @@ -16,10 +25,18 @@ restrictNumeric = (e) -> input = String.fromCharCode(e.which) + console.log !!/[\d\s]/.test(input) + # Char is a number or a space !!/[\d\s]/.test(input) restrictAlphaNumeric = (e) -> + + console.log 'restrictAlphaNumeric triggered' + $target = $(e.target) + console.log '"' + $target.val() + '"' + return true + # Key event is for a browser shortcut return true if e.metaKey or e.ctrlKey @@ -34,6 +51,8 @@ restrictAlphaNumeric = (e) -> input = String.fromCharCode(e.which) + console.log !!/[\d\sA-Za-z]/.test(input) + # Char is a number or a space !!/[\d\sA-Za-z]/.test(input) @@ -48,13 +67,23 @@ hasTextSelected = ($target) -> false -$.fn.formance.restrictNumeric = -> +$.formance.fn.restrictNumeric = -> + console.log 'Setting up keypress listener for restrictNumeric' @on('keypress', restrictNumeric) this -$.fn.formance.restrictAlphaNumeric = -> + +$.formance.fn.restrictAlphaNumeric = -> + console.log 'Setting up keypress listener for restrictAlphaNumeric' @on('keypress', restrictAlphaNumeric) this -$.fn.formance.hasTextSelected = hasTextSelected +$.formance.fn.hasTextSelected = hasTextSelected -require('./fields/*') +require('./fields/credit_card_cvc') +require('./fields/credit_card_expiry') +require('./fields/credit_card_number') +require('./fields/date') +require('./fields/number') +require('./fields/ontario_photo_health_card_number') +require('./fields/phone_number') +require('./fields/postal_code') \ No newline at end of file diff --git a/src/jquery.formatters.coffee b/src/jquery.formatters.coffee deleted file mode 100644 index b985cbf..0000000 --- a/src/jquery.formatters.coffee +++ /dev/null @@ -1,352 +0,0 @@ -require('./jquery.payment') - -$ = jQuery -$.formatters = {} -$.formatters.fn = {} -$.fn.formatters = (method, args...) -> - $.formatters.fn[method].apply(this, args) - -hasTextSelected = ($target) -> - # If some text is selected - return true if $target.prop('selectionStart')? and - $target.prop('selectionStart') isnt $target.prop('selectionEnd') - - # If some text is selected in IE - return true if document?.selection?.createRange?().text - - false - -restrictNumeric = (e) -> - # Key event is for a browser shortcut - return true if e.metaKey or e.ctrlKey - - # If keycode is a space - return false if e.which is 32 - - # If keycode is a special char (WebKit) - return true if e.which is 0 - - # If char is a special char (Firefox) - return true if e.which < 33 - - input = String.fromCharCode(e.which) - - # Char is a number or a space - !!/[\d\s]/.test(input) - -restrictAlphaNumeric = (e) -> - # Key event is for a browser shortcut - return true if e.metaKey or e.ctrlKey - - # If keycode is a space - return false if e.which is 32 - - # If keycode is a special char (WebKit) - return true if e.which is 0 - - # If char is a special char (Firefox) - return true if e.which < 33 - - input = String.fromCharCode(e.which) - - # Char is a number or a space - !!/[\d\sA-Za-z]/.test(input) - - -# DATE FUNCTIONS -restrictDate = (e) -> - $target = $(e.currentTarget) - digit = String.fromCharCode(e.which) - return unless /^\d+$/.test(digit) - - return if hasTextSelected($target) - - value = $target.val() + digit - value = value.replace(/\D/g, '') - - return false if value.length > 8 - -formatDate = (e) -> - # Only format if input is a number - digit = String.fromCharCode(e.which) - return unless /^\d+$/.test(digit) - - $target = $(e.currentTarget) - old_val = $target.val() - val = old_val + digit - - if /^\d$/.test(val) and digit not in ['0', '1', '2', '3'] - e.preventDefault() - $target.val("0#{val} / ") - - else if /^\d{2}$/.test(val) - e.preventDefault() - $target.val("#{val} / ") - - else if /^\d{2}\s\/\s\d$/.test(val) and digit not in ['0', '1'] - e.preventDefault() - $target.val("#{old_val}0#{digit} / ") - - else if /^\d{2}\s\/\s\d{2}$/.test(val) - e.preventDefault() - $target.val("#{val} / ") - -formatForwardDate = (e) -> #handles when the user enters the second digit - digit = String.fromCharCode(e.which) - return unless /^\d+$/.test(digit) - - $target = $(e.currentTarget) - val = $target.val() - - # handles when entering the 2nd and 4th digits - if /^\d{2}$/.test(val) or /^\d{2}\s\/\s\d{2}$/.test(val) - $target.val("#{val} / ") - -formatForwardSlash = (e) -> # handles when the user hits '/' - slash = String.fromCharCode(e.which) - return unless slash is '/' - - $target = $(e.currentTarget) - val = $target.val() - - parse_day = /^(\d)$/ - parse_month = /^(\d{2})\s\/\s(\d)$/ - - if parse_day.test(val) and val isnt '0' - $target.val("0#{val} / ") - else if parse_month.test(val) - [date, day, month] = val.match(parse_month) - if month isnt '0' - $target.val("#{day} / 0#{month} / ") - -formatBackDate = (e) -> - # If shift+backspace is pressed - return if e.meta - - $target = $(e.currentTarget) - value = $target.val() - - # Return unless backspacing - return unless e.which is 8 - - # Return if focus isn't at the end of the text - return if $target.prop('selectionStart')? and - $target.prop('selectionStart') isnt value.length - - # Remove the trailing space - if /\d(\s|\/)+$/.test(value) - e.preventDefault() - $target.val(value.replace(/\d(\s|\/)*$/, '')) - else if /\s\/\s?\d?$/.test(value) - e.preventDefault() - $target.val(value.replace(/\s\/\s?\d?$/, '')) - - -# PHONE NUMBER FUNCTIONS -reFormatPhoneNumber = (phone_number_string) -> - [phone_number, area_code, first3, last4] = phone_number_string.replace(/\D/g, '').match(/^(\d{0,3})?(\d{0,3})?(\d{0,4})?$/) - - text = '' - text += "(#{area_code}" if area_code? - text += ") " if area_code?.length is 3 - - text += "#{first3}" if first3? - text += " - " if first3?.length is 3 - - text += "#{last4}" if last4? - return text - -restrictPhoneNumber = (e) -> - $target = $(e.currentTarget) - digit = String.fromCharCode(e.which) - return unless /^\d+$/.test(digit) - - return if hasTextSelected($target) - - value = $target.val() + digit - value = value.replace(/\D/g, '') - - return false if value.length > 10 - -formatPhoneNumber = (e) -> - digit = String.fromCharCode(e.which) - return unless /^\d+$/.test(digit) - - $target = $(e.currentTarget) - val = $target.val() + digit - - text = reFormatPhoneNumber(val) - - e.preventDefault() - $target.val(text) - -formatBackPhoneNumber = (e) -> - # If shift+backspace is pressed - return if e.meta - - $target = $(e.currentTarget) - value = $target.val() - - # Return unless backspacing - return unless e.which is 8 - - # Return if focus isn't at the end of the text - return if $target.prop('selectionStart')? and - $target.prop('selectionStart') isnt value.length - - # Removes trailing spaces,brackets and dashes when - # hitting backspace at one of the jumps - if /\(\d$/.test(value) - e.preventDefault() - $target.val('') - else if /\d\)(\s)+$/.test(value) - e.preventDefault() - $target.val(value.replace(/\d\)(\s)*$/, '')) - else if /\d(\s|\-)+$/.test(value) - e.preventDefault() - $target.val(value.replace(/\d(\s|\-)+$/, '')) - -formatPastePhoneNumber = (e) -> - setTimeout => # it takes a bit of time for the paste event to add the input, so wait a bit - $target = $(e.currentTarget) - val = $target.val() - - text = reFormatPhoneNumber(val) - $target.val(text) - -# POSTAL CODE FUNCTIONS - -restrictPostalCode = (e) -> - $target = $(e.currentTarget) - char = String.fromCharCode(e.which) - - return unless /^[a-zA-Z\d]+$/.test(char) - - return if hasTextSelected($target) - - value = $target.val() + char - value = value.replace(/[^a-zA-Z\d]/g, '') - - return false if value.length > 6 - -formatPostalCode = (e) -> - char = String.fromCharCode(e.which) - return unless /^[a-zA-Z\d]+$/.test(char) - - $target = $(e.currentTarget) - old_val = $target.val() - val = old_val + char.toUpperCase() - - if old_val is '' - e.preventDefault() - $target.val(val) if /^[ABCEFGHJKLMNPRSTVXY]$/.test(val) - - else if /^[ABCEFGHJKLMNPRSTVXY]$/.test(old_val) - e.preventDefault() - $target.val(val) if /^[ABCEFGHJKLMNPRSTVXY][0-9]$/.test(val) - - else if /^[ABCEFGHJKLMNPRSTVXY][0-9]$/.test(old_val) - e.preventDefault() - $target.val("#{val} ") if /^[ABCEFGHJKLMNPRSTVXY][0-9][ABCEFGHJKLMNPRSTVWXYZ]$/.test(val) - - else if /^[ABCEFGHJKLMNPRSTVXY][0-9][ABCEFGHJKLMNPRSTVWXYZ]\s$/.test(old_val) - e.preventDefault() - $target.val(val) if /^[ABCEFGHJKLMNPRSTVXY][0-9][ABCEFGHJKLMNPRSTVWXYZ]\s?[0-9]$/.test(val) - - else if /^[ABCEFGHJKLMNPRSTVXY][0-9][ABCEFGHJKLMNPRSTVWXYZ]\s?[0-9]$/.test(old_val) - e.preventDefault() - $target.val(val) if /^[ABCEFGHJKLMNPRSTVXY][0-9][ABCEFGHJKLMNPRSTVWXYZ]\s?[0-9][ABCEFGHJKLMNPRSTVWXYZ]$/.test(val) - - else if /^[ABCEFGHJKLMNPRSTVXY][0-9][ABCEFGHJKLMNPRSTVWXYZ]\s?[0-9][ABCEFGHJKLMNPRSTVWXYZ]$/.test(old_val) - e.preventDefault() - $target.val(val) if /^[ABCEFGHJKLMNPRSTVXY][0-9][ABCEFGHJKLMNPRSTVWXYZ]\s?[0-9][ABCEFGHJKLMNPRSTVWXYZ][0-9]$/.test(val) - -formatBackPostalCode = (e) -> - # If shift+backspace is pressed - return if e.meta - - $target = $(e.currentTarget) - value = $target.val() - - # Return unless backspacing - return unless e.which is 8 - - # Return if focus isn't at the end of the text - return if $target.prop('selectionStart')? and - $target.prop('selectionStart') isnt value.length - - # Removes trailing spaces,brackets and dashes when - # hitting backspace at one of the jumps - if /[ABCEFGHJKLMNPRSTVWXYZ](\s)+$/.test(value) - e.preventDefault() - $target.val(value.replace(/[ABCEFGHJKLMNPRSTVWXYZ](\s)*$/, '')) - -formatPastePostalCode = (e) -> - setTimeout => # it takes a bit of time for the paste event to add the input, so wait a bit - $target = $(e.currentTarget) - val = $target.val() - - [full, first_part, second_part] = val.match(/^([ABCEFGHJKLMNPRSTVXY][0-9][ABCEFGHJKLMNPRSTVWXYZ])\s?([0-9][ABCEFGHJKLMNPRSTVWXYZ][0-9])$/) - $target.val("#{first_part} #{second_part}") - - -# FORMATTERS - -$.formatters.fn.formatCardNumber = -> - @.payment('formatCardNumber') - this - -$.formatters.fn.formatCardExpiry = -> - @.payment('formatCardExpiry') - this - -$.formatters.fn.formatCardCVC = -> - @.payment('formatCardCVC') - this - -$.formatters.fn.formatDate = -> - @formatters('restrictNumeric') - @on('keypress', restrictDate) - @on('keypress', formatDate) - @on('keypress', formatForwardSlash) - @on('keypress', formatForwardDate) - @on('keydown', formatBackDate) - this - -# $.formatters.fn.formatEmail = -> -# this - -$.formatters.fn.formatNumber = -> - @formatters('restrictNumeric') - this - -$.formatters.fn.formatOntarioHealthCardNumber = -> - @formatters('restrictAlphaNumeric') - @on('keypress', restrictOntarioPhotoHealthCardNumber) - @on('keypress', formatOntarioPhotoHealthCardNumber) - @on('keydown', formatBackOntarioPhotoHealthCardNumber) - @on('paste', formatPasteOntarioPhotoHealthCardNumber) - this - -$.formatters.fn.formatPhoneNumber = -> - @formatters('restrictNumeric') - @on('keypress', restrictPhoneNumber) - @on('keypress', formatPhoneNumber) - @on('keydown', formatBackPhoneNumber) - @on('paste', formatPastePhoneNumber) - this - -$.formatters.fn.formatPostalCode = -> - @formatters('restrictAlphaNumeric') - @on('keypress', restrictPostalCode) - @on('keypress', formatPostalCode) - @on('keydown', formatBackPostalCode) - @on('paste', formatPastePostalCode) - this - -$.formatters.fn.restrictNumeric = -> - @on('keypress', restrictNumeric) - this -$.formatters.fn.restrictAlphaNumeric = -> - @on('keypress', restrictAlphaNumeric) - this diff --git a/src/jquery.payment.coffee b/src/jquery.payment.coffee deleted file mode 100644 index 58c827f..0000000 --- a/src/jquery.payment.coffee +++ /dev/null @@ -1,439 +0,0 @@ -$ = jQuery -$.payment = {} -$.payment.fn = {} -$.fn.payment = (method, args...) -> - $.payment.fn[method].apply(this, args) - -# Utils - -defaultFormat = /(\d{1,4})/g - -cards = [ - { - type: 'maestro' - pattern: /^(5018|5020|5038|6304|6759|676[1-3])/ - format: defaultFormat - length: [12..19] - cvcLength: [3] - luhn: true - } - { - type: 'dinersclub' - pattern: /^(36|38|30[0-5])/ - format: defaultFormat - length: [14] - cvcLength: [3] - luhn: true - } - { - type: 'laser' - pattern: /^(6706|6771|6709)/ - format: defaultFormat - length: [16..19] - cvcLength: [3] - luhn: true - } - { - type: 'jcb' - pattern: /^35/ - format: defaultFormat - length: [16] - cvcLength: [3] - luhn: true - } - { - type: 'unionpay' - pattern: /^62/ - format: defaultFormat - length: [16..19] - cvcLength: [3] - luhn: false - } - { - type: 'discover' - pattern: /^(6011|65|64[4-9]|622)/ - format: defaultFormat - length: [16] - cvcLength: [3] - luhn: true - } - { - type: 'mastercard' - pattern: /^5[1-5]/ - format: defaultFormat - length: [16] - cvcLength: [3] - luhn: true - } - { - type: 'amex' - pattern: /^3[47]/ - format: /(\d{1,4})(\d{1,6})?(\d{1,5})?/ - length: [15] - cvcLength: [3..4] - luhn: true - } - { - type: 'visa' - pattern: /^4/ - format: defaultFormat - length: [13..16] - cvcLength: [3] - luhn: true - } -] - -cardFromNumber = (num) -> - num = (num + '').replace(/\D/g, '') - return card for card in cards when card.pattern.test(num) - -cardFromType = (type) -> - return card for card in cards when card.type is type - -luhnCheck = (num) -> - odd = true - sum = 0 - - digits = (num + '').split('').reverse() - - for digit in digits - digit = parseInt(digit, 10) - digit *= 2 if (odd = !odd) - digit -= 9 if digit > 9 - sum += digit - - sum % 10 == 0 - -hasTextSelected = ($target) -> - # If some text is selected - return true if $target.prop('selectionStart')? and - $target.prop('selectionStart') isnt $target.prop('selectionEnd') - - # If some text is selected in IE - return true if document?.selection?.createRange?().text - - false - -# Private - -# Format Card Number - -reFormatCardNumber = (e) -> - setTimeout => - $target = $(e.currentTarget) - value = $target.val() - value = $.payment.formatCardNumber(value) - $target.val(value) - -formatCardNumber = (e) -> - # Only format if input is a number - digit = String.fromCharCode(e.which) - return unless /^\d+$/.test(digit) - - $target = $(e.currentTarget) - value = $target.val() - card = cardFromNumber(value + digit) - length = (value.replace(/\D/g, '') + digit).length - - upperLength = 16 - upperLength = card.length[card.length.length - 1] if card - return if length >= upperLength - - # Return if focus isn't at the end of the text - return if $target.prop('selectionStart')? and - $target.prop('selectionStart') isnt value.length - - if card && card.type is 'amex' - # Amex cards are formatted differently - re = /^(\d{4}|\d{4}\s\d{6})$/ - else - re = /(?:^|\s)(\d{4})$/ - - # If '4242' + 4 - if re.test(value) - e.preventDefault() - $target.val(value + ' ' + digit) - - # If '424' + 2 - else if re.test(value + digit) - e.preventDefault() - $target.val(value + digit + ' ') - -formatBackCardNumber = (e) -> - $target = $(e.currentTarget) - value = $target.val() - - return if e.meta - - # Return unless backspacing - return unless e.which is 8 - - # Return if focus isn't at the end of the text - return if $target.prop('selectionStart')? and - $target.prop('selectionStart') isnt value.length - - # Remove the trailing space - if /\d\s$/.test(value) - e.preventDefault() - $target.val(value.replace(/\d\s$/, '')) - else if /\s\d?$/.test(value) - e.preventDefault() - $target.val(value.replace(/\s\d?$/, '')) - -# Format Expiry - -formatExpiry = (e) -> - # Only format if input is a number - digit = String.fromCharCode(e.which) - return unless /^\d+$/.test(digit) - - $target = $(e.currentTarget) - val = $target.val() + digit - - if /^\d$/.test(val) and val not in ['0', '1'] - e.preventDefault() - $target.val("0#{val} / ") - - else if /^\d\d$/.test(val) - e.preventDefault() - $target.val("#{val} / ") - -formatForwardExpiry = (e) -> - digit = String.fromCharCode(e.which) - return unless /^\d+$/.test(digit) - - $target = $(e.currentTarget) - val = $target.val() - - if /^\d\d$/.test(val) - $target.val("#{val} / ") - -formatForwardSlash = (e) -> - slash = String.fromCharCode(e.which) - return unless slash is '/' - - $target = $(e.currentTarget) - val = $target.val() - - if /^\d$/.test(val) and val isnt '0' - $target.val("0#{val} / ") - -formatBackExpiry = (e) -> - # If shift+backspace is pressed - return if e.meta - - $target = $(e.currentTarget) - value = $target.val() - - # Return unless backspacing - return unless e.which is 8 - - # Return if focus isn't at the end of the text - return if $target.prop('selectionStart')? and - $target.prop('selectionStart') isnt value.length - - # Remove the trailing space - if /\d(\s|\/)+$/.test(value) - e.preventDefault() - $target.val(value.replace(/\d(\s|\/)*$/, '')) - else if /\s\/\s?\d?$/.test(value) - e.preventDefault() - $target.val(value.replace(/\s\/\s?\d?$/, '')) - -# Restrictions - -restrictNumeric = (e) -> - # Key event is for a browser shortcut - return true if e.metaKey or e.ctrlKey - - # If keycode is a space - return false if e.which is 32 - - # If keycode is a special char (WebKit) - return true if e.which is 0 - - # If char is a special char (Firefox) - return true if e.which < 33 - - input = String.fromCharCode(e.which) - - # Char is a number or a space - !!/[\d\s]/.test(input) - -restrictCardNumber = (e) -> - $target = $(e.currentTarget) - digit = String.fromCharCode(e.which) - return unless /^\d+$/.test(digit) - - return if hasTextSelected($target) - - # Restrict number of digits - value = ($target.val() + digit).replace(/\D/g, '') - card = cardFromNumber(value) - - if card - value.length <= card.length[card.length.length - 1] - else - # All other cards are 16 digits long - value.length <= 16 - -restrictExpiry = (e) -> - $target = $(e.currentTarget) - digit = String.fromCharCode(e.which) - return unless /^\d+$/.test(digit) - - return if hasTextSelected($target) - - value = $target.val() + digit - value = value.replace(/\D/g, '') - - return false if value.length > 6 - -restrictCVC = (e) -> - $target = $(e.currentTarget) - digit = String.fromCharCode(e.which) - return unless /^\d+$/.test(digit) - - val = $target.val() + digit - val.length <= 4 - -setCardType = (e) -> - $target = $(e.currentTarget) - val = $target.val() - cardType = $.payment.cardType(val) or 'unknown' - - unless $target.hasClass(cardType) - allTypes = (card.type for card in cards) - - $target.removeClass('unknown') - $target.removeClass(allTypes.join(' ')) - - $target.addClass(cardType) - $target.toggleClass('identified', cardType isnt 'unknown') - $target.trigger('payment.cardType', cardType) - -# Public - -# Formatting - -$.payment.fn.formatCardCVC = -> - @payment('restrictNumeric') - @on('keypress', restrictCVC) - this - -$.payment.fn.formatCardExpiry = -> - @payment('restrictNumeric') - @on('keypress', restrictExpiry) - @on('keypress', formatExpiry) - @on('keypress', formatForwardSlash) - @on('keypress', formatForwardExpiry) - @on('keydown', formatBackExpiry) - this - -$.payment.fn.formatCardNumber = -> - @payment('restrictNumeric') - @on('keypress', restrictCardNumber) - @on('keypress', formatCardNumber) - @on('keydown', formatBackCardNumber) - @on('keyup', setCardType) - @on('paste', reFormatCardNumber) - this - -# Restrictions - -$.payment.fn.restrictNumeric = -> - @on('keypress', restrictNumeric) - this - -# Validations - -$.payment.fn.cardExpiryVal = -> - $.payment.cardExpiryVal($(this).val()) - -$.payment.cardExpiryVal = (value) -> - value = value.replace(/\s/g, '') - [month, year] = value.split('/', 2) - - # Allow for year shortcut - if year?.length is 2 and /^\d+$/.test(year) - prefix = (new Date).getFullYear() - prefix = prefix.toString()[0..1] - year = prefix + year - - month = parseInt(month, 10) - year = parseInt(year, 10) - - month: month, year: year - -$.payment.validateCardNumber = (num) -> - num = (num + '').replace(/\s+|-/g, '') - return false unless /^\d+$/.test(num) - - card = cardFromNumber(num) - return false unless card - - num.length in card.length and - (card.luhn is false or luhnCheck(num)) - -$.payment.validateCardExpiry = (month, year) => - # Allow passing an object - if typeof month is 'object' and 'month' of month - {month, year} = month - - return false unless month and year - - month = $.trim(month) - year = $.trim(year) - - return false unless /^\d+$/.test(month) - return false unless /^\d+$/.test(year) - return false unless parseInt(month, 10) <= 12 - - if year.length is 2 - prefix = (new Date).getFullYear() - prefix = prefix.toString()[0..1] - year = prefix + year - - expiry = new Date(year, month) - currentTime = new Date - - # Months start from 0 in JavaScript - expiry.setMonth(expiry.getMonth() - 1) - - # The cc expires at the end of the month, - # so we need to make the expiry the first day - # of the month after - expiry.setMonth(expiry.getMonth() + 1, 1) - - expiry > currentTime - -$.payment.validateCardCVC = (cvc, type) -> - cvc = $.trim(cvc) - return false unless /^\d+$/.test(cvc) - - if type - # Check against a explicit card type - cvc.length in cardFromType(type)?.cvcLength - else - # Check against all types - cvc.length >= 3 and cvc.length <= 4 - -$.payment.cardType = (num) -> - return null unless num - cardFromNumber(num)?.type or null - -$.payment.formatCardNumber = (num) -> - card = cardFromNumber(num) - return num unless card - - upperLength = card.length[card.length.length - 1] - - num = num.replace(/\D/g, '') - num = num[0..upperLength] - - if card.format.global - num.match(card.format)?.join(' ') - else - groups = card.format.exec(num) - groups?.shift() - groups?.join(' ') diff --git a/src/jquery.validators.coffee b/src/jquery.validators.coffee deleted file mode 100644 index bfde711..0000000 --- a/src/jquery.validators.coffee +++ /dev/null @@ -1,87 +0,0 @@ -require('./jquery.payment') - -$ = jQuery -$.validators = {} -$.validators.fn = {} -$.fn.validators = (method, args...) -> - $.validators.fn[method].apply(this, args) - - -$.validators.validateCardNumber = (num) -> - return $.payment.validateCardNumber(num) - -$.validators.validateCardExpiry = (expiry_string) -> - expiry = $.payment.cardExpiryVal(expiry_string) - return $.payment.validateCardExpiry(expiry.month, expiry.year) - -$.validators.validateCardCVC = (cvc, type) -> - return $.payment.validateCardCVC(cvc, type) - - - -$.validators.validateDate = (day, month, year) -> #day, month, year to stay consistent with expiry which is month, year - return false if not day? or isNaN(day) or not month? or isNaN(month) or not year? or isNaN(year) - return false unless (0 < day <= 31) and (0 < month <= 12) and (1000 < year <= 10000) #can probably use better logic for 'valid' year - return true - - -# email is one of those tough things to validate, and probably best accomplished by -# sending a verification email -# $.validators.validateEmail = (email) -> -# return true - - -$.validators.validateNumber = (num) -> - return /^\d+$/.test(num) - - -$.validators.validateOntarioPhotoHealthCardNumber = (str) -> - return false unless str? - - # 1234 - 123 - 123 - AB - regex = /^(\d{4})[\s|\-]*?(\d{3})[\s|\-]*?(\d{3})[\s|\-]*?([A-Za-z]{2})$/ - string = str.replace(/[^a-zA-Z\d]/g, '') - - return regex.test(str) - - -#can't handle extensions for now -$.validators.validatePhoneNumber = (phone_string) -> - phone_string = phone_string.replace(/\(|\)|\s+|-/g, '') - return false unless /^\d+$/.test(phone_string) - - # [area_code, first_three, last_four] = phone_string.match(/\d+/g) - return phone_string.replace(/\D/g, '').length is 10 # replaces all non digits [^0-9] with '' - - -$.validators.validatePostalCode = (postal_code_string) -> - return false unless postal_code_string? - postal_code_string = postal_code_string.replace(/\s+/g, '') - return false unless /^[a-zA-Z\d]+$/.test(postal_code_string) - - # http://stackoverflow.com/questions/1146202/canada-postal-code-validation - # apparently some letters are restricted - # - first letter can't be D,I,O,Q,U,W,Z - # - second letter can't be D,I,O,Q,U - # - third letter can't be D,I,O,Q,U - postal_code_string = postal_code_string.replace(/[^a-zA-Z\d]/g, '') #\W allows certain special characters - /^[ABCEFGHJKLMNPRSTVXY][0-9][ABCEFGHJKLMNPRSTVWXYZ]\s?[0-9][ABCEFGHJKLMNPRSTVWXYZ][0-9]$/.test(postal_code_string.toUpperCase()) - - - -$.validators.fn.dateVal = -> - $.validators.dateVal($(this).val()) - -$.validators.dateVal = (date_string) -> - [day, month, year] = if date_string? then date_string.replace(/\s/g, '').split('/', 3) else [NaN, NaN, NaN] - - day = parseInt(day, 10) - month = parseInt(month, 10) - year = parseInt(year, 10) - - return day: day, month: month, year: year - -$.validators.fn.cardExpiryVal =-> - $validators.cardExpiryVal($(this).val()) - -$.validators.cardExpiryVal = $.payment.cardExpiryVal \ No newline at end of file diff --git a/test/fields/credit_card_cvc.coffee b/test/fields/credit_card_cvc.coffee new file mode 100644 index 0000000..3de6519 --- /dev/null +++ b/test/fields/credit_card_cvc.coffee @@ -0,0 +1,58 @@ +assert = require('assert') +$ = require('jquery') +global.jQuery = $ + + +require('../lib/jquery.formance.js') + + +describe 'credit_card_cvc.js', -> + + describe 'Validating a CVC without credit card type', -> + it 'should fail if is empty', -> + topic = $.formance.validateCreditCardCVC '' + assert.equal topic, false + + it 'should pass if is valid', -> + topic = $.formance.validateCreditCardCVC '123' + assert.equal topic, true + + it 'should fail with non-digits', -> + topic = $.formance.validateCreditCardCVC '12e' + assert.equal topic, false + + it 'should fail with less than 3 digits', -> + topic = $.formance.validateCreditCardCVC '12' + assert.equal topic, false + + it 'should fail with more than 4 digits', -> + topic = $.formance.validateCreditCardCVC '12345' + assert.equal topic, false + + describe 'Validating a CVC with credit card type', -> + + it 'should validate a three digit number with no card type', -> + topic = $.formance.validateCreditCardCVC('123') + assert.equal topic, true + + it 'should validate a three digit number with card type amex', -> + topic = $.formance.validateCreditCardCVC('123', 'amex') + assert.equal topic, true + + it 'should validate a three digit number with card type other than amex', -> + topic = $.formance.validateCreditCardCVC('123', 'visa') + assert.equal topic, true + + it 'should not validate a four digit number with a card type other than amex', -> + topic = $.formance.validateCreditCardCVC('1234', 'visa') + assert.equal topic, false + + it 'should validate a four digit number with card type amex', -> + topic = $.formance.validateCreditCardCVC('1234', 'amex') + assert.equal topic, true + + it 'should not validate a number larger than 4 digits', -> + topic = $.formance.validateCreditCardCVC('12344') + assert.equal topic, false + + diff --git a/test/fields/credit_card_expiry.coffee b/test/fields/credit_card_expiry.coffee new file mode 100644 index 0000000..5fbd11b --- /dev/null +++ b/test/fields/credit_card_expiry.coffee @@ -0,0 +1,108 @@ +assert = require('assert') +$ = require('jquery') +global.jQuery = $ + + +require('../lib/jquery.formance.js') + + +describe 'credit_card_expiry.js', -> + + + describe 'formatCreditCardExpiry', -> + + it 'should format month shorthand correctly', -> + $expiry = $('').formance('formatCreditCardExpiry') + + e = $.Event('keypress'); + e.which = 52 # '4' + $expiry.trigger(e) + + assert.equal $expiry.val(), '04 / ' + + it 'should format forward slash shorthand correctly', -> + $expiry = $('').formance('formatCreditCardExpiry') + $expiry.val('1') + + e = $.Event('keypress'); + e.which = 47 # '/' + $expiry.trigger(e) + + assert.equal $expiry.val(), '01 / ' + + it 'should only allow numbers', -> + $expiry = $('').formance('formatCreditCardExpiry') + $expiry.val('1') + + e = $.Event('keypress'); + e.which = 100 # 'd' + $expiry.trigger(e) + + assert.equal $expiry.val(), '1' + + + describe 'Validating an expiration date', -> + + it 'should fail expires is before the current year', -> + currentTime = new Date() + topic = $.formance.validateCreditCardExpiry currentTime.getMonth() + 1, currentTime.getFullYear() - 1 + assert.equal topic, false + + it 'that expires in the current year but before current month', -> + currentTime = new Date() + topic = $.formance.validateCreditCardExpiry currentTime.getMonth(), currentTime.getFullYear() + assert.equal topic, false + + it 'that has an invalid month', -> + currentTime = new Date() + topic = $.formance.validateCreditCardExpiry 13, currentTime.getFullYear() + assert.equal topic, false + + it 'that is this year and month', -> + currentTime = new Date() + topic = $.formance.validateCreditCardExpiry currentTime.getMonth() + 1, currentTime.getFullYear() + assert.equal topic, true + + it 'that is just after this month', -> + # Remember - months start with 0 in JavaScript! + currentTime = new Date() + topic = $.formance.validateCreditCardExpiry currentTime.getMonth() + 1, currentTime.getFullYear() + assert.equal topic, true + + it 'that is after this year', -> + currentTime = new Date() + topic = $.formance.validateCreditCardExpiry currentTime.getMonth() + 1, currentTime.getFullYear() + 1 + assert.equal topic, true + + it 'that has string numbers', -> + currentTime = new Date() + currentTime.setFullYear(currentTime.getFullYear() + 1, currentTime.getMonth() + 2) + topic = $.formance.validateCreditCardExpiry currentTime.getMonth() + '', currentTime.getFullYear() + '' + assert.equal topic, true + + it 'that has non-numbers', -> + topic = $.formance.validateCreditCardExpiry 'h12', '3300' + assert.equal topic, false + + it 'should fail if year or month is NaN', -> + topic = $.formance.validateCreditCardExpiry '12', NaN + assert.equal topic, false + + it 'should support year shorthand', -> + assert.equal $.formance.validateCreditCardExpiry('05', '20'), true + + + + describe 'Parsing an expiry value', -> + + it 'should parse string expiry', -> + topic = $.formance.creditCardExpiryVal('03 / 2025') + assert.deepEqual topic, month: 3, year: 2025 + + it 'should support shorthand year', -> + topic = $.formance.creditCardExpiryVal('05/04') + assert.deepEqual topic, month: 5, year: 2004 + + it 'should return NaN when it cannot parse', -> + topic = $.formance.creditCardExpiryVal('05/dd') + assert isNaN(topic.year) diff --git a/test/fields/credit_card_number.coffee b/test/fields/credit_card_number.coffee new file mode 100644 index 0000000..a8ff8d0 --- /dev/null +++ b/test/fields/credit_card_number.coffee @@ -0,0 +1,137 @@ +assert = require('assert') +$ = require('jquery') +global.jQuery = $ + + +require('../lib/jquery.formance.js') + + +describe 'credit_card_number.js', -> + + describe 'formatCreditCardNumber', -> + + it 'should format cc number correctly', -> + $number = $('').formance('formatCreditCardNumber') + $number.val('4242') + + e = $.Event('keypress'); + e.which = 52 # '4' + $number.trigger(e) + + assert.equal $number.val(), '4242 4' + + + describe 'Validating a card number', -> + + it 'should fail if empty', -> + topic = $.formance.validateCreditCardNumber '' + assert.equal topic, false + + it 'should fail if is a bunch of spaces', -> + topic = $.formance.validateCreditCardNumber ' ' + assert.equal topic, false + + it 'should success if is valid', -> + topic = $.formance.validateCreditCardNumber '4242424242424242' + assert.equal topic, true + + it 'that has dashes in it but is valid', -> + topic = $.formance.validateCreditCardNumber '4242-4242-4242-4242' + assert.equal topic, true + + it 'should succeed if it has spaces in it but is valid', -> + topic = $.formance.validateCreditCardNumber '4242 4242 4242 4242' + assert.equal topic, true + + it 'that does not pass the luhn checker', -> + topic = $.formance.validateCreditCardNumber '4242424242424241' + assert.equal topic, false + + it 'should fail if is more than 16 digits', -> + topic = $.formance.validateCreditCardNumber '42424242424242424' + assert.equal topic, false + + it 'should fail if is less than 10 digits', -> + topic = $.formance.validateCreditCardNumber '424242424' + assert.equal topic, false + + it 'should fail with non-digits', -> + topic = $.formance.validateCreditCardNumber '4242424e42424241' + assert.equal topic, false + + it 'should validate for all card types', -> + assert($.formance.validateCreditCardNumber('378282246310005'), 'amex') + assert($.formance.validateCreditCardNumber('371449635398431'), 'amex') + assert($.formance.validateCreditCardNumber('378734493671000'), 'amex') + + assert($.formance.validateCreditCardNumber('30569309025904'), 'dinersclub') + assert($.formance.validateCreditCardNumber('38520000023237'), 'dinersclub') + + assert($.formance.validateCreditCardNumber('6011111111111117'), 'discover') + assert($.formance.validateCreditCardNumber('6011000990139424'), 'discover') + + assert($.formance.validateCreditCardNumber('3530111333300000'), 'jcb') + assert($.formance.validateCreditCardNumber('3566002020360505'), 'jcb') + + assert($.formance.validateCreditCardNumber('5555555555554444'), 'mastercard') + + assert($.formance.validateCreditCardNumber('4111111111111111'), 'visa') + assert($.formance.validateCreditCardNumber('4012888888881881'), 'visa') + assert($.formance.validateCreditCardNumber('4222222222222'), 'visa') + + assert($.formance.validateCreditCardNumber('6759649826438453'), 'maestro') + + assert($.formance.validateCreditCardNumber('6271136264806203568'), 'unionpay') + assert($.formance.validateCreditCardNumber('6236265930072952775'), 'unionpay') + assert($.formance.validateCreditCardNumber('6204679475679144515'), 'unionpay') + assert($.formance.validateCreditCardNumber('6216657720782466507'), 'unionpay') + + + describe 'Getting a card type', -> + + it 'should return Visa that begins with 40', -> + topic = $.formance.creditCardType '4012121212121212' + assert.equal topic, 'visa' + + it 'that begins with 5 should return MasterCard', -> + topic = $.formance.creditCardType '5555555555554444' + assert.equal topic, 'mastercard' + + it 'that begins with 34 should return American Express', -> + topic = $.formance.creditCardType '3412121212121212' + assert.equal topic, 'amex' + + it 'that is not numbers should return null', -> + topic = $.formance.creditCardType 'aoeu' + assert.equal topic, null + + it 'that has unrecognized beginning numbers should return null', -> + topic = $.formance.creditCardType 'aoeu' + assert.equal topic, null + + it 'should return correct type for all test numbers', -> + assert.equal($.formance.creditCardType('378282246310005'), 'amex') + assert.equal($.formance.creditCardType('371449635398431'), 'amex') + assert.equal($.formance.creditCardType('378734493671000'), 'amex') + + assert.equal($.formance.creditCardType('30569309025904'), 'dinersclub') + assert.equal($.formance.creditCardType('38520000023237'), 'dinersclub') + + assert.equal($.formance.creditCardType('6011111111111117'), 'discover') + assert.equal($.formance.creditCardType('6011000990139424'), 'discover') + + assert.equal($.formance.creditCardType('3530111333300000'), 'jcb') + assert.equal($.formance.creditCardType('3566002020360505'), 'jcb') + + assert.equal($.formance.creditCardType('5555555555554444'), 'mastercard') + + assert.equal($.formance.creditCardType('4111111111111111'), 'visa') + assert.equal($.formance.creditCardType('4012888888881881'), 'visa') + assert.equal($.formance.creditCardType('4222222222222'), 'visa') + + assert.equal($.formance.creditCardType('6759649826438453'), 'maestro') + + assert.equal($.formance.creditCardType('6271136264806203568'), 'unionpay') + assert.equal($.formance.creditCardType('6236265930072952775'), 'unionpay') + assert.equal($.formance.creditCardType('6204679475679144515'), 'unionpay') + assert.equal($.formance.creditCardType('6216657720782466507'), 'unionpay') diff --git a/test/fields/date.coffee b/test/fields/date.coffee new file mode 100644 index 0000000..d8d88af --- /dev/null +++ b/test/fields/date.coffee @@ -0,0 +1,167 @@ +assert = require('assert') +$ = require('jquery') +global.jQuery = $ + + +require('../lib/jquery.formance.js') + + +describe 'date.js', -> + + describe 'formatDate', -> + + it 'should format day correctly single digit', -> + $date = $('').formance('formatDate') + + e = $.Event('keypress') + e.which = 52 # '4' + $date.trigger(e) + + assert.equal $date.val(), '04 / ' + + it 'should format day correctly double digit', -> + $date = $('').formance('formatDate') + $date.val('2') + + e = $.Event('keypress') + e.which = 52 # '4' + $date.trigger(e) + + assert.equal $date.val(), '24 / ' + + it 'should format forward slash shorthand correctly', -> + $date = $('').formance('formatDate') + $date.val('1') + + e = $.Event('keypress'); + e.which = 47 # '/' + $date.trigger(e) + + assert.equal $date.val(), '01 / ' + + it 'should format month correctly single digit', -> + $date = $('').formance('formatDate') + $date.val('04 / ') + + e = $.Event('keypress') + e.which = 57 # '9' + $date.trigger(e) + + assert.equal $date.val(), '04 / 09 / ' + + it 'should format day correctly double digit', -> + $date = $('').formance('formatDate') + $date.val('24 / 1') + + e = $.Event('keypress') + e.which = 50 # '2' + $date.trigger(e) + + assert.equal $date.val(), '24 / 12 / ' + + it 'should format forward slash shorthand correctly', -> + $date = $('').formance('formatDate') + $date.val('24 / 1') + + e = $.Event('keypress'); + e.which = 47 # '/' + $date.trigger(e) + + assert.equal $date.val(), '24 / 01 / ' + + it 'should only allow numbers', -> + $date = $('').formance('formatDate') + $date.val('01 / 12 / ') + + e = $.Event('keypress'); + e.which = 100 # 'd' + $date.trigger(e) + + assert.equal $date.val(), '01 / 12 / ' + + + describe 'Validating a date', -> + + it 'that is a valid day', -> + date = new Date() + topic = $.formance.validateDate date.getDay(), date.getMonth(), date.getFullYear() + assert.equal topic, true + + it 'that has string numbers', -> + date = new Date() + topic = $.formance.validateDate date.getDay()+'', date.getMonth()+'', date.getFullYear()+'' + assert.equal topic, true + + it 'that has an invalid day', -> + # TODO can have better logic, for example selecting 31 of february + topic = $.formance.validateDate -1, 12, 2013 + assert.equal topic, false + + topic = $.formance.validateDate 32, 12, 2013 + assert.equal topic, false + + it 'that has an invalid month', -> + topic = $.formance.validateDate 1, -11, 2013 + assert.equal topic, false + + topic = $.formance.validateDate 1, 13, 2013 + assert.equal topic, false + + it 'that has an invalid year', -> + topic = $.formance.validateDate 1, 12, -2013 + assert.equal topic, false + + it 'should not support year shorthand', -> + topic = $.formance.validateDate 1, 12, 13 + assert.equal topic, false + + it 'should fail if any value is NaN', -> + topic = $.formance.validateDate NaN, 12, 13 + assert.equal topic, false + + topic = $.formance.validateDate 1, NaN, 13 + assert.equal topic, false + + topic = $.formance.validateDate 1, 12, NaN + assert.equal topic, false + + + describe 'Parsing a date', -> + + it 'should parse a date string', -> + topic = $.formance.dateVal('01 / 07 / 2013') + assert.deepEqual topic, {day: 1, month: 7, year: 2013} + + # it is up to the validator to determine if it is a legitimate date + it 'should parse if less than 8 digits', -> + topic = $.formance.dateVal '1 / 07 / 2013' + assert.deepEqual topic, {day: 1, month: 7, year: 2013} + + topic = $.formance.dateVal '01 / 07 / 201' + assert.deepEqual topic, {day: 1, month: 7, year: 201} + + # it is up to the validator to determine if it is a legitimate date + it 'should parse if more than 8 digits', -> + topic = $.formance.dateVal '011 / 07 / 2013' + assert.deepEqual topic, {day: 11, month: 7, year: 2013} + + topic = $.formance.dateVal '01 / 072 / 2013' + assert.deepEqual topic, {day: 1, month: 72, year: 2013} + + topic = $.formance.dateVal '01 / 07 / 20133' + assert.deepEqual topic, {day: 1, month: 7, year: 20133} + + it 'should return NaN when it cannot parse', -> + topic = $.formance.dateVal('dd / 07 / 2013') + assert.equal !!topic.day, false + + topic = $.formance.dateVal('01 / mm / 2013') + assert.equal !!topic.month, false + + topic = $.formance.dateVal('01 / 07 / yyyy') #not '01 / 07 / 20yy' will work + assert.equal !!topic.year, false + + topic = $.formance.dateVal(' ') + assert.equal !!topic.day, false + assert.equal !!topic.month, false + assert.equal !!topic.year, false diff --git a/test/fields/number.coffee b/test/fields/number.coffee new file mode 100644 index 0000000..f28f635 --- /dev/null +++ b/test/fields/number.coffee @@ -0,0 +1,54 @@ +assert = require('assert') +$ = require('jquery') +global.jQuery = $ + + +require('../lib/jquery.formance.js') + + +describe 'number.js', -> + + + describe 'formatNumber', -> + + it 'should allow numbers', -> + $number = $('').formance('formatPhoneNumber') + $number.val('123') + + e = $.Event('keypress'); + e.which = 52 # '4' + $number.trigger(e) + + assert.equal $number.val(), '1234' + + + it 'should not allow non-digits', -> + $number = $('').formance('formatPhoneNumber') + $number.val('123') + + e = $.Event('keypress'); + e.which = 100 # 'd' + $number.trigger(e) + + assert.equal $number.val(), '123' + + + describe 'Validating a number', -> + + it 'should fail if empty', -> + topic = $.formance.validateNumber '' + assert.equal topic, false + + it 'should fail if it is a bunch of spaces', -> + topic = $.formance.validateNumber ' ' + assert.equal topic, false + + it 'should succeed if valid', -> + topic = $.formance.validateNumber '12344' + assert.equal topic, true + + it 'should fail with non digits', -> + topic = $.formance.validateNumber '123zaas' + assert.equal topic, false + + diff --git a/test/fields/ontario_photo_health_card_number.coffee b/test/fields/ontario_photo_health_card_number.coffee new file mode 100644 index 0000000..91f581a --- /dev/null +++ b/test/fields/ontario_photo_health_card_number.coffee @@ -0,0 +1,78 @@ +assert = require('assert') +$ = require('jquery') +global.jQuery = $ + + +require('../lib/jquery.formance.js') + + +describe 'ontario_photo_health_card_number.js', -> + + + describe 'formatOntarioPhotoHealthCardNumber', -> + + it 'should format first four digits correctly', -> + $ophcn = $('').formance('formatOntarioPhotoHealthCardNumber') + $ophcn.val('123') + + e = $.Event('keypress') + e.which = '52' + $ophcn.trigger(e) + + assert.equal $ophcn.val(), '1234 - ' + + it 'should format second three correctly', -> + $ophcn = $('').formance('formatOntarioPhotoHealthCardNumber') + $ophcn.val('1234 - 12') + + e = $.Event('keypress') + e.which = '51' # '3' + $ophcn.trigger(e) + + assert.equal $ophcn.val(), '1234 - 123 - ' + + it 'should format third three correctly', -> + $ophcn = $('').formance('formatOntarioPhotoHealthCardNumber') + $ophcn.val('1234 - 123 - 12') + + e = $.Event('keypress') + e.which = '51' # '3' + $ophcn.trigger(e) + + assert.equal $ophcn.val(), '1234 - 123 - 123 - ' + + # it 'should only allow alphanumeric characters', -> + + # it 'should only allow letters for last 2 characters', -> + + # add tests for backspacing + + + describe 'Validating an ontario photo health card number', -> + + it 'should fail if empty', -> + topic = $.formance.validateOntarioPhotoHealthCardNumber '' + assert.equal topic, false + + it 'should fail ig it is a bunch of space', -> + topic = $.formance.validateOntarioPhotoHealthCardNumber ' ' + assert.equal topic, false + + it 'should succeed if valid', -> + topic = $.formance.validateOntarioPhotoHealthCardNumber '1234123123AB' + assert.equal topic, true + + it 'has dashes and spaces but is valid', -> + topic = $.formance.validateOntarioPhotoHealthCardNumber '1234 - 123 - 123 - AB' + assert.equal topic, true + + it 'should fail if more than 12 characters', -> + topic = $.formance.validateOntarioPhotoHealthCardNumber '1234 - 123 - 123 - ABC' + assert.equal topic, false + + topic = $.formance.validateOntarioPhotoHealthCardNumber '1234 - 1233 - 123 - AB' + assert.equal topic, false + + it 'should fail with non alphanumeric characters', -> + topic = $.formance.validateOntarioPhotoHealthCardNumber '1234; - 123 - ;/123 -/ AB' + assert.equal topic, false \ No newline at end of file diff --git a/test/fields/phone_number.coffee b/test/fields/phone_number.coffee new file mode 100644 index 0000000..5c1054a --- /dev/null +++ b/test/fields/phone_number.coffee @@ -0,0 +1,88 @@ +assert = require('assert') +$ = require('jquery') +global.jQuery = $ + + +require('../lib/jquery.formance.js') + + +describe 'phone_number.js', -> + + describe 'formatPhoneNumber', -> + + it 'should format first digit correctly', -> + $phone_number = $('').formance('formatPhoneNumber') + + e = $.Event('keypress') + e.which = '52' + $phone_number.trigger(e) + + assert.equal $phone_number.val(), '(4' + + it 'should format area code correctly', -> + $phone_number = $('').formance('formatPhoneNumber') + $phone_number.val('(61') + + e = $.Event('keypress') + e.which = '52' + $phone_number.trigger(e) + + assert.equal $phone_number.val(), '(614) ' + + it 'should format first three correctly', -> + $phone_number = $('').formance('formatPhoneNumber') + $phone_number.val('(614) 12') + + e = $.Event('keypress') + e.which = '52' + $phone_number.trigger(e) + + assert.equal $phone_number.val(), '(614) 124 - ' + + it 'should only allow numbers', -> + $phone_number = $('').formance('formatPhoneNumber') + $phone_number.val('(61') + + e = $.Event('keypress'); + e.which = 100 # 'd' + $phone_number.trigger(e) + + assert.equal $phone_number.val(), '(61' + + # add tests for backspacing + + + describe 'Validating a phone number', -> + + it 'should fail if empty', -> + topic = $.formance.validatePhoneNumber '' + assert.equal topic, false + + it 'should fail if it is a bunch of spaces', -> + topic = $.formance.validatePhoneNumber ' ' + assert.equal topic, false + + it 'should succeed if valid', -> + topic = $.formance.validatePhoneNumber '6137384446' + assert.equal topic, true + + it 'has spaces but is valid', -> + topic = $.formance.validatePhoneNumber '613 738 4446' + assert.equal topic, true + + it 'has brackets and dashes but is valid', -> + topic = $.formance.validatePhoneNumber '(613) 738 - 4446' + assert.equal topic, true + + it 'should fail if more than 10 digits', -> + topic = $.formance.validatePhoneNumber '(123) 456 - 78901' + assert.equal topic, false + + it 'should fail if less than 10 digits', -> + topic = $.formance.validatePhoneNumber '(123) 456 - 789' + assert.equal topic, false + + it 'should fail with non digits', -> + topic = $.formance.validatePhoneNumber '(123) er456 - 1232' + assert.equal topic, false + diff --git a/test/fields/postal_code.coffee b/test/fields/postal_code.coffee new file mode 100644 index 0000000..4842f2f --- /dev/null +++ b/test/fields/postal_code.coffee @@ -0,0 +1,82 @@ +assert = require('assert') +$ = require('jquery') +global.jQuery = $ + + +require('../lib/jquery.formance.js') + + +describe 'postal_code.js', -> + + + describe 'formatPostalCode', -> + + it 'should format postal code correctly', -> + $postal_code = $('').formance('formatPostalCode') + $postal_code.val('K1') + + e = $.Event('keypress') + e.which = 72 # 'H' + $postal_code.trigger(e) + + assert.equal $postal_code.val(), 'K1H ' + + it 'should try to insert a letter in place of a number', -> + $postal_code = $('').formance('formatPostalCode') + $postal_code.val('K1H ') + + e = $.Event('keypress') + e.which = 72 # 'H' + $postal_code.trigger(e) + + assert.equal $postal_code.val(), 'K1H ' + + it 'should try to insert a number in place of a letter', -> + $postal_code = $('').formance('formatPostalCode') + $postal_code.val('K1H 8') + + e = $.Event('keypress') + e.which = 56 # '8' + $postal_code.trigger(e) + + assert.equal $postal_code.val(), 'K1H 8' + + + + describe 'Validating a postal code', -> + + it 'should fail if empty', -> + topic = $.formance.validatePostalCode '' + assert.equal topic, false + + it 'should fail if it is a bunch of spaces', -> + topic = $.formance.validatePostalCode ' ' + assert.equal topic, false + + it 'should succeed if valid', -> + topic = $.formance.validatePostalCode 'k1h8k9' + assert.equal topic, true + + topic = $.formance.validatePostalCode 'k1h 8k9' + assert.equal topic, true + + it 'should fail if less than 6 characters', -> + topic = $.formance.validatePostalCode 'k1h 8k' + assert.equal topic, false + + topic = $.formance.validatePostalCode 'k1 8k9' + assert.equal topic, false + + it 'should fail if more than 6 characters', -> + topic = $.formance.validatePostalCode 'kk1h 8k9' + assert.equal topic, false + + topic = $.formance.validatePostalCode 'k1h 8k91' + assert.equal topic, false + + it 'should fail with non alphanumeric characters', -> + topic = $.formance.validatePostalCode 'k1h-8k9' + assert.equal topic, false + + topic = $.formance.validatePostalCode 'k1h;8k9' + assert.equal topic, false \ No newline at end of file diff --git a/test/formatters.coffee b/test/formatters.coffee deleted file mode 100644 index 7ce227c..0000000 --- a/test/formatters.coffee +++ /dev/null @@ -1,213 +0,0 @@ -assert = require('assert') -$ = require('jquery') -global.jQuery = $ - - -require('../src/jquery.formatters') - -describe 'formatters.js', -> - - describe 'formatDate', -> - it 'should format day correctly single digit', -> - $date = $('').formatters('formatDate') - - e = $.Event('keypress') - e.which = 52 # '4' - $date.trigger(e) - - assert.equal $date.val(), '04 / ' - - it 'should format day correctly double digit', -> - $date = $('').formatters('formatDate') - $date.val('2') - - e = $.Event('keypress') - e.which = 52 # '4' - $date.trigger(e) - - assert.equal $date.val(), '24 / ' - - it 'should format forward slash shorthand correctly', -> - $date = $('').formatters('formatDate') - $date.val('1') - - e = $.Event('keypress'); - e.which = 47 # '/' - $date.trigger(e) - - assert.equal $date.val(), '01 / ' - - it 'should format month correctly single digit', -> - $date = $('').formatters('formatDate') - $date.val('04 / ') - - e = $.Event('keypress') - e.which = 57 # '9' - $date.trigger(e) - - assert.equal $date.val(), '04 / 09 / ' - - it 'should format day correctly double digit', -> - $date = $('').formatters('formatDate') - $date.val('24 / 1') - - e = $.Event('keypress') - e.which = 50 # '2' - $date.trigger(e) - - assert.equal $date.val(), '24 / 12 / ' - - it 'should format forward slash shorthand correctly', -> - $date = $('').formatters('formatDate') - $date.val('24 / 1') - - e = $.Event('keypress'); - e.which = 47 # '/' - $date.trigger(e) - - assert.equal $date.val(), '24 / 01 / ' - - it 'should only allow numbers', -> - $date = $('').formatters('formatDate') - $date.val('01 / 12 / ') - - e = $.Event('keypress'); - e.which = 100 # 'd' - $date.trigger(e) - - assert.equal $date.val(), '01 / 12 / ' - - - describe 'formatNumber', -> - it 'should only allow numbers', -> - $number = $('').formatters('formatPhoneNumber') - $number.val('123') - - e = $.Event('keypress'); - e.which = 100 # 'd' - $number.trigger(e) - - assert.equal $number.val(), '123' - - - - - describe 'formatOntarioHealthCardNumber', -> - it 'should format first four digits correctly', -> - $ophc_number = $('').formatters('formatOntarioHealthCardNumber') - $ophc_number.val('123') - - e = $.Event('keypress') - e.which = '52' - $ophc_number.trigger(e) - - assert.equal $ophc_number.val(), '1234 - ' - - it 'should format second three correctly', -> - $ophc_number = $('').formatters('formatOntarioHealthCardNumber') - $ophc_number.val('1234 - 12') - - e = $.Event('keypress') - e.which = '51' # '3' - $ophc_number.trigger(e) - - assert.equal $ophc_number.val(), '1234 - 123 - ' - - it 'should format third three correctly', -> - $ophc_number = $('').formatters('formatOntarioHealthCardNumber') - $ophc_number.val('1234 - 123 - 12') - - console.log 'herere ------------- ' - console.log $ophc_number.val() - - e = $.Event('keypress') - e.which = '51' # '3' - $ophc_number.trigger(e) - - - console.log $ophc_number.val() - - - assert.equal $ophc_number.val(), '1234 - 123 - 123 - ' - - # it 'should only allow alphanumeric characters', -> - - # it 'should only allow letters for last 2 characters', -> - - # add tests for backspacing - - - - describe 'formatPhoneNumber', -> - it 'should format first digit correctly', -> - $phone_number = $('').formatters('formatPhoneNumber') - - e = $.Event('keypress') - e.which = '52' - $phone_number.trigger(e) - - assert.equal $phone_number.val(), '(4' - - it 'should format area code correctly', -> - $phone_number = $('').formatters('formatPhoneNumber') - $phone_number.val('(61') - - e = $.Event('keypress') - e.which = '52' - $phone_number.trigger(e) - - assert.equal $phone_number.val(), '(614) ' - - it 'should format first three correctly', -> - $phone_number = $('').formatters('formatPhoneNumber') - $phone_number.val('(614) 12') - - e = $.Event('keypress') - e.which = '52' - $phone_number.trigger(e) - - assert.equal $phone_number.val(), '(614) 124 - ' - - it 'should only allow numbers', -> - $phone_number = $('').formatters('formatPhoneNumber') - $phone_number.val('(61') - - e = $.Event('keypress'); - e.which = 100 # 'd' - $phone_number.trigger(e) - - assert.equal $phone_number.val(), '(61' - - # add tests for backspacing - - - describe 'formatPostalCode', -> - it 'should format postal code correctly', -> - $postal_code = $('').formatters('formatPostalCode') - $postal_code.val('K1') - - e = $.Event('keypress') - e.which = 72 # 'H' - $postal_code.trigger(e) - - assert.equal $postal_code.val(), 'K1H ' - - it 'should try to insert a letter in place of a number', -> - $postal_code = $('').formatters('formatPostalCode') - $postal_code.val('K1H ') - - e = $.Event('keypress') - e.which = 72 # 'H' - $postal_code.trigger(e) - - assert.equal $postal_code.val(), 'K1H ' - - it 'should try to insert a number in place of a letter', -> - $postal_code = $('').formatters('formatPostalCode') - $postal_code.val('K1H 8') - - e = $.Event('keypress') - e.which = 56 # '8' - $postal_code.trigger(e) - - assert.equal $postal_code.val(), 'K1H 8' \ No newline at end of file diff --git a/test/formance.coffee b/test/jquery.formance.coffee similarity index 55% rename from test/formance.coffee rename to test/jquery.formance.coffee index bd98eb0..bd837f6 100644 --- a/test/formance.coffee +++ b/test/jquery.formance.coffee @@ -3,71 +3,71 @@ $ = require('jquery') global.jQuery = $ +require('../lib/jquery.formance.js') -require('../src/jquery.formance') - -describe 'formance.js', -> +describe 'jquery.formance.js', -> describe 'restrictNumeric', -> it 'should allow digits to be entered', -> - $field = $('').formance.restrictNumeric() + $field = $('').formance('restrictNumeric') + $field.val('123') e = $.Event('keypress') e.which = 52 # '4' $field.trigger(e) + + console.log '"' + $field.val() + '"' - assert.equal $date.val(), '4' - + assert.equal $field.val(), '1234' it 'should restrict letters from being entered', -> - $field = $('').formance.restrictNumeric() + $field = $('').formance('restrictNumeric') e = $.Event('keypress') e.which = 68 # 'd' $field.trigger(e) - assert.equal $date.val(), '' + assert.equal $field.val(), '' it 'should restrict special characters from being entered', -> - $field = $('').formance.restrictNumeric() + $field = $('').formance('restrictNumeric') e = $.Event('keypress') e.which = 189 # '-' $field.trigger(e) - assert.equal $date.val(), '' + assert.equal $field.val(), '' describe 'restrictAlphaNumeric', -> it 'should allow digits to be entered', -> - $field = $('').formance.restrictAlphaNumeric() + $field = $('').formance('restrictAlphaNumeric') e = $.Event('keypress') e.which = 52 # '4' $field.trigger(e) - assert.equal $date.val(), '4' - + assert.equal $field.val(), '4' it 'should allow letters to be entered', -> - $field = $('').formance.restrictAlphaNumeric() + $field = $('').formance('restrictAlphaNumeric') e = $.Event('keypress') e.which = 68 # 'd' $field.trigger(e) - assert.equal $date.val(), 'd' + assert.equal $field.val(), 'd' it 'should restrict special characters from being entered', -> - $field = $('').formance.restrictAlphaNumeric() + $field = $('').formance('restrictAlphaNumeric') e = $.Event('keypress') e.which = 189 # '-' $field.trigger(e) - assert.equal $date.val(), '' \ No newline at end of file + assert.equal $field.val(), '' \ No newline at end of file diff --git a/test/payment.coffee b/test/payment.coffee deleted file mode 100644 index 622d94e..0000000 --- a/test/payment.coffee +++ /dev/null @@ -1,267 +0,0 @@ -assert = require('assert') -$ = require('jquery') -global.jQuery = $ - -require('../src/jquery.payment') - -describe 'jquery.payment', -> - describe 'Validating a card number', -> - it 'should fail if empty', -> - topic = $.payment.validateCardNumber '' - assert.equal topic, false - - it 'should fail if is a bunch of spaces', -> - topic = $.payment.validateCardNumber ' ' - assert.equal topic, false - - it 'should success if is valid', -> - topic = $.payment.validateCardNumber '4242424242424242' - assert.equal topic, true - - it 'that has dashes in it but is valid', -> - topic = $.payment.validateCardNumber '4242-4242-4242-4242' - assert.equal topic, true - - it 'should succeed if it has spaces in it but is valid', -> - topic = $.payment.validateCardNumber '4242 4242 4242 4242' - assert.equal topic, true - - it 'that does not pass the luhn checker', -> - topic = $.payment.validateCardNumber '4242424242424241' - assert.equal topic, false - - it 'should fail if is more than 16 digits', -> - topic = $.payment.validateCardNumber '42424242424242424' - assert.equal topic, false - - it 'should fail if is less than 10 digits', -> - topic = $.payment.validateCardNumber '424242424' - assert.equal topic, false - - it 'should fail with non-digits', -> - topic = $.payment.validateCardNumber '4242424e42424241' - assert.equal topic, false - - it 'should validate for all card types', -> - assert($.payment.validateCardNumber('378282246310005'), 'amex') - assert($.payment.validateCardNumber('371449635398431'), 'amex') - assert($.payment.validateCardNumber('378734493671000'), 'amex') - - assert($.payment.validateCardNumber('30569309025904'), 'dinersclub') - assert($.payment.validateCardNumber('38520000023237'), 'dinersclub') - - assert($.payment.validateCardNumber('6011111111111117'), 'discover') - assert($.payment.validateCardNumber('6011000990139424'), 'discover') - - assert($.payment.validateCardNumber('3530111333300000'), 'jcb') - assert($.payment.validateCardNumber('3566002020360505'), 'jcb') - - assert($.payment.validateCardNumber('5555555555554444'), 'mastercard') - - assert($.payment.validateCardNumber('4111111111111111'), 'visa') - assert($.payment.validateCardNumber('4012888888881881'), 'visa') - assert($.payment.validateCardNumber('4222222222222'), 'visa') - - assert($.payment.validateCardNumber('6759649826438453'), 'maestro') - - assert($.payment.validateCardNumber('6271136264806203568'), 'unionpay') - assert($.payment.validateCardNumber('6236265930072952775'), 'unionpay') - assert($.payment.validateCardNumber('6204679475679144515'), 'unionpay') - assert($.payment.validateCardNumber('6216657720782466507'), 'unionpay') - - describe 'Validating a CVC', -> - it 'should fail if is empty', -> - topic = $.payment.validateCardCVC '' - assert.equal topic, false - - it 'should pass if is valid', -> - topic = $.payment.validateCardCVC '123' - assert.equal topic, true - - it 'should fail with non-digits', -> - topic = $.payment.validateCardNumber '12e' - assert.equal topic, false - - it 'should fail with less than 3 digits', -> - topic = $.payment.validateCardNumber '12' - assert.equal topic, false - - it 'should fail with more than 4 digits', -> - topic = $.payment.validateCardNumber '12345' - assert.equal topic, false - - describe 'Validating an expiration date', -> - it 'should fail expires is before the current year', -> - currentTime = new Date() - topic = $.payment.validateCardExpiry currentTime.getMonth() + 1, currentTime.getFullYear() - 1 - assert.equal topic, false - - it 'that expires in the current year but before current month', -> - currentTime = new Date() - topic = $.payment.validateCardExpiry currentTime.getMonth(), currentTime.getFullYear() - assert.equal topic, false - - it 'that has an invalid month', -> - currentTime = new Date() - topic = $.payment.validateCardExpiry 13, currentTime.getFullYear() - assert.equal topic, false - - it 'that is this year and month', -> - currentTime = new Date() - topic = $.payment.validateCardExpiry currentTime.getMonth() + 1, currentTime.getFullYear() - assert.equal topic, true - - it 'that is just after this month', -> - # Remember - months start with 0 in JavaScript! - currentTime = new Date() - topic = $.payment.validateCardExpiry currentTime.getMonth() + 1, currentTime.getFullYear() - assert.equal topic, true - - it 'that is after this year', -> - currentTime = new Date() - topic = $.payment.validateCardExpiry currentTime.getMonth() + 1, currentTime.getFullYear() + 1 - assert.equal topic, true - - it 'that has string numbers', -> - currentTime = new Date() - currentTime.setFullYear(currentTime.getFullYear() + 1, currentTime.getMonth() + 2) - topic = $.payment.validateCardExpiry currentTime.getMonth() + '', currentTime.getFullYear() + '' - assert.equal topic, true - - it 'that has non-numbers', -> - topic = $.payment.validateCardExpiry 'h12', '3300' - assert.equal topic, false - - it 'should fail if year or month is NaN', -> - topic = $.payment.validateCardExpiry '12', NaN - assert.equal topic, false - - it 'should support year shorthand', -> - assert.equal $.payment.validateCardExpiry('05', '20'), true - - describe 'Validating a CVC number', -> - it 'should validate a three digit number with no card type', -> - topic = $.payment.validateCardCVC('123') - assert.equal topic, true - - it 'should validate a three digit number with card type amex', -> - topic = $.payment.validateCardCVC('123', 'amex') - assert.equal topic, true - - it 'should validate a three digit number with card type other than amex', -> - topic = $.payment.validateCardCVC('123', 'visa') - assert.equal topic, true - - it 'should not validate a four digit number with a card type other than amex', -> - topic = $.payment.validateCardCVC('1234', 'visa') - assert.equal topic, false - - it 'should validate a four digit number with card type amex', -> - topic = $.payment.validateCardCVC('1234', 'amex') - assert.equal topic, true - - it 'should not validate a number larger than 4 digits', -> - topic = $.payment.validateCardCVC('12344') - assert.equal topic, false - - describe 'Parsing an expiry value', -> - it 'should parse string expiry', -> - topic = $.payment.cardExpiryVal('03 / 2025') - assert.deepEqual topic, month: 3, year: 2025 - - it 'should support shorthand year', -> - topic = $.payment.cardExpiryVal('05/04') - assert.deepEqual topic, month: 5, year: 2004 - - it 'should return NaN when it cannot parse', -> - topic = $.payment.cardExpiryVal('05/dd') - assert isNaN(topic.year) - - describe 'Getting a card type', -> - it 'should return Visa that begins with 40', -> - topic = $.payment.cardType '4012121212121212' - assert.equal topic, 'visa' - - it 'that begins with 5 should return MasterCard', -> - topic = $.payment.cardType '5555555555554444' - assert.equal topic, 'mastercard' - - it 'that begins with 34 should return American Express', -> - topic = $.payment.cardType '3412121212121212' - assert.equal topic, 'amex' - - it 'that is not numbers should return null', -> - topic = $.payment.cardType 'aoeu' - assert.equal topic, null - - it 'that has unrecognized beginning numbers should return null', -> - topic = $.payment.cardType 'aoeu' - assert.equal topic, null - - it 'should return correct type for all test numbers', -> - assert.equal($.payment.cardType('378282246310005'), 'amex') - assert.equal($.payment.cardType('371449635398431'), 'amex') - assert.equal($.payment.cardType('378734493671000'), 'amex') - - assert.equal($.payment.cardType('30569309025904'), 'dinersclub') - assert.equal($.payment.cardType('38520000023237'), 'dinersclub') - - assert.equal($.payment.cardType('6011111111111117'), 'discover') - assert.equal($.payment.cardType('6011000990139424'), 'discover') - - assert.equal($.payment.cardType('3530111333300000'), 'jcb') - assert.equal($.payment.cardType('3566002020360505'), 'jcb') - - assert.equal($.payment.cardType('5555555555554444'), 'mastercard') - - assert.equal($.payment.cardType('4111111111111111'), 'visa') - assert.equal($.payment.cardType('4012888888881881'), 'visa') - assert.equal($.payment.cardType('4222222222222'), 'visa') - - assert.equal($.payment.cardType('6759649826438453'), 'maestro') - - assert.equal($.payment.cardType('6271136264806203568'), 'unionpay') - assert.equal($.payment.cardType('6236265930072952775'), 'unionpay') - assert.equal($.payment.cardType('6204679475679144515'), 'unionpay') - assert.equal($.payment.cardType('6216657720782466507'), 'unionpay') - - describe 'formatCardNumber', -> - it 'should format cc number correctly', -> - $number = $('').payment('formatCardNumber') - $number.val('4242') - - e = $.Event('keypress'); - e.which = 52 # '4' - $number.trigger(e) - - assert.equal $number.val(), '4242 4' - - describe 'formatCardExpiry', -> - it 'should format month shorthand correctly', -> - $expiry = $('').payment('formatCardExpiry') - - e = $.Event('keypress'); - e.which = 52 # '4' - $expiry.trigger(e) - - assert.equal $expiry.val(), '04 / ' - - it 'should format forward slash shorthand correctly', -> - $expiry = $('').payment('formatCardExpiry') - $expiry.val('1') - - e = $.Event('keypress'); - e.which = 47 # '/' - $expiry.trigger(e) - - assert.equal $expiry.val(), '01 / ' - - it 'should only allow numbers', -> - $expiry = $('').payment('formatCardExpiry') - $expiry.val('1') - - e = $.Event('keypress'); - e.which = 100 # 'd' - $expiry.trigger(e) - - assert.equal $expiry.val(), '1' diff --git a/test/untitled b/test/untitled deleted file mode 100644 index 06948c4..0000000 --- a/test/untitled +++ /dev/null @@ -1,12 +0,0 @@ -# $(this).formance.ontarioPhotoHealthCard.format() -# .formance.ontarioPhotoHealthCard.validate() - -# $(this).formance.phoneNumber.format() -# .formance.phoneNumber.validate() - -# $(this).formance.creditCardNumber.format() -# .formance.creditCardNumber.validate() - -# expiry = $.formance.creditCardExpiry.val() - -# $.formance.creditCardExpiry.validate() diff --git a/test/validators.coffee b/test/validators.coffee deleted file mode 100644 index a245e91..0000000 --- a/test/validators.coffee +++ /dev/null @@ -1,239 +0,0 @@ -assert = require('assert') -$ = require('jquery') -global.jQuery = $ - - -require('../src/jquery.validators') - -describe 'validators.js', -> - - # CVC validations - describe 'Validating a CVC', -> - it 'should fail if is empty', -> - topic = $.validators.validateCardCVC '' - assert.equal topic, false - - it 'should pass if is valid', -> - topic = $.validators.validateCardCVC '123' - assert.equal topic, true - - it 'should fail with non-digits', -> - topic = $.validators.validateCardNumber '12e' - assert.equal topic, false - - it 'should fail with less than 3 digits', -> - topic = $.validators.validateCardNumber '12' - assert.equal topic, false - - it 'should fail with more than 4 digits', -> - topic = $.validators.validateCardNumber '12345' - assert.equal topic, false - - - describe 'Validating a date', -> - - it 'that is a valid day', -> - date = new Date() - topic = $.validators.validateDate date.getDay(), date.getMonth(), date.getFullYear() - assert.equal topic, true - - it 'that has string numbers', -> - date = new Date() - topic = $.validators.validateDate date.getDay()+'', date.getMonth()+'', date.getFullYear()+'' - assert.equal topic, true - - it 'that has an invalid day', -> - # TODO can have better logic, for example selecting 31 of february - topic = $.validators.validateDate -1, 12, 2013 - assert.equal topic, false - - topic = $.validators.validateDate 32, 12, 2013 - assert.equal topic, false - - it 'that has an invalid month', -> - topic = $.validators.validateDate 1, -11, 2013 - assert.equal topic, false - - topic = $.validators.validateDate 1, 13, 2013 - assert.equal topic, false - - it 'that has an invalid year', -> - topic = $.validators.validateDate 1, 12, -2013 - assert.equal topic, false - - it 'should not support year shorthand', -> - topic = $.validators.validateDate 1, 12, 13 - assert.equal topic, false - - it 'should fail if any value is NaN', -> - topic = $.validators.validateDate NaN, 12, 13 - assert.equal topic, false - - topic = $.validators.validateDate 1, NaN, 13 - assert.equal topic, false - - topic = $.validators.validateDate 1, 12, NaN - assert.equal topic, false - - - - describe 'Validating a number', -> - it 'should fail if empty', -> - topic = $.validators.validateNumber '' - assert.equal topic, false - - it 'should fail if it is a bunch of spaces', -> - topic = $.validators.validateNumber ' ' - assert.equal topic, false - - it 'should succeed if valid', -> - topic = $.validators.validateNumber '12344' - assert.equal topic, true - - it 'should fail with non digits', -> - topic = $.validators.validateNumber '123zaas' - assert.equal topic, false - - - - describe 'Validating an ontario photo health card number', -> - it 'should fail if empty', -> - topic = $.validators.validateOntarioPhotoHealthCardNumber '' - assert.equal topic, false - - it 'should fail ig it is a bunch of space', -> - topic = $.validators.validateOntarioPhotoHealthCardNumber ' ' - assert.equal topic, false - - it 'should succeed if valid', -> - topic = $.validators.validateOntarioPhotoHealthCardNumber '1234123123AB' - assert.equal topic, true - - it 'has dashes and spaces but is valid', -> - topic = $.validators.validateOntarioPhotoHealthCardNumber '1234 - 123 - 123 - AB' - assert.equal topic, true - - it 'should fail if more than 12 characters', -> - topic = $.validators.validateOntarioPhotoHealthCardNumber '1234 - 123 - 123 - ABC' - assert.equal topic, false - topic = $.validators.validateOntarioPhotoHealthCardNumber '1234 - 1233 - 123 - AB' - assert.equal topic, false - - it 'should fail with non alphanumeric characters', -> - topic = $.validators.validateOntarioPhotoHealthCardNumber '1234; - 123 - ;/123 -/ AB' - assert.equal topic, false - - - - describe 'Validating a phone number', -> - it 'should fail if empty', -> - topic = $.validators.validatePhoneNumber '' - assert.equal topic, false - - it 'should fail if it is a bunch of spaces', -> - topic = $.validators.validatePhoneNumber - - it 'should succeed if valid', -> - topic = $.validators.validatePhoneNumber '6137384446' - assert.equal topic, true - - it 'has spaces but is valid', -> - topic = $.validators.validatePhoneNumber '613 738 4446' - assert.equal topic, true - - it 'has brackets and dashes but is valid', -> - topic = $.validators.validatePhoneNumber '(613) 738 - 4446' - assert.equal topic, true - - it 'should fail if more than 10 digits', -> - topic = $.validators.validatePhoneNumber '(123) 456 - 78901' - assert.equal topic, false - - it 'should fail if less than 10 digits', -> - topic = $.validators.validatePhoneNumber '(123) 456 - 789' - assert.equal topic, false - - it 'should fail with non digits', -> - topic = $.validators.validatePhoneNumber '(123) er456 - 1232' - assert.equal topic, false - - - - describe 'Validating a postal code', -> - it 'should fail if empty', -> - topic = $.validators.validatePostalCode '' - assert.equal topic, false - - it 'should fail if it is a bunch of spaces', -> - topic = $.validators.validatePostalCode ' ' - assert.equal topic, false - - it 'should succeed if valid', -> - topic = $.validators.validatePostalCode 'k1h8k9' - assert.equal topic, true - - topic = $.validators.validatePostalCode 'k1h 8k9' - assert.equal topic, true - - it 'should fail if less than 6 characters', -> - topic = $.validators.validatePostalCode 'k1h 8k' - assert.equal topic, false - - topic = $.validators.validatePostalCode 'k1 8k9' - assert.equal topic, false - - it 'should fail if more than 6 characters', -> - topic = $.validators.validatePostalCode 'kk1h 8k9' - assert.equal topic, false - - topic = $.validators.validatePostalCode 'k1h 8k91' - assert.equal topic, false - - it 'should fail with non alphanumeric characters', -> - topic = $.validators.validatePostalCode 'k1h-8k9' - assert.equal topic, false - - topic = $.validators.validatePostalCode 'k1h;8k9' - assert.equal topic, false - - - - describe 'Parsing a date', -> - it 'should parse a date string', -> - topic = $.validators.dateVal('01 / 07 / 2013') - assert.deepEqual topic, {day: 1, month: 7, year: 2013} - - # it is up to the validator to determine if it is a legitimate date - it 'should parse if less than 8 digits', -> - topic = $.validators.dateVal '1 / 07 / 2013' - assert.deepEqual topic, {day: 1, month: 7, year: 2013} - - topic = $.validators.dateVal '01 / 07 / 201' - assert.deepEqual topic, {day: 1, month: 7, year: 201} - - # it is up to the validator to determine if it is a legitimate date - it 'should parse if more than 8 digits', -> - topic = $.validators.dateVal '011 / 07 / 2013' - assert.deepEqual topic, {day: 11, month: 7, year: 2013} - - topic = $.validators.dateVal '01 / 072 / 2013' - assert.deepEqual topic, {day: 1, month: 72, year: 2013} - - topic = $.validators.dateVal '01 / 07 / 20133' - assert.deepEqual topic, {day: 1, month: 7, year: 20133} - - it 'should return NaN when it cannot parse', -> - topic = $.validators.dateVal('dd / 07 / 2013') - assert.equal !!topic.day, false - - topic = $.validators.dateVal('01 / mm / 2013') - assert.equal !!topic.month, false - - topic = $.validators.dateVal('01 / 07 / yyyy') #not '01 / 07 / 20yy' will work - assert.equal !!topic.year, false - - topic = $.validators.dateVal(' ') - assert.equal !!topic.day, false - assert.equal !!topic.month, false - assert.equal !!topic.year, false -