// Generated by CoffeeScript 1.6.3 (function() { var $, CHECK_DIGIT_POSITION_WEIGHTS, formattingRules, hasTextSelected, restrictCharPattern, restrictLength, validate, validationRules, validations; $ = jQuery; $.bank = {}; CHECK_DIGIT_POSITION_WEIGHTS = [3, 7, 1]; formattingRules = { US: { RoutingTransitNumber: { length: 9, charPattern: /\d/ } }, JP: { BankNumber: { length: 4, charPattern: /\d/ }, BranchNumber: { length: 3, charPattern: /\d/ } } }; validationRules = { US: { RoutingTransitNumber: { pattern: /^(0[0-9]|1[0-2]|2[1-9]|3[0-2]|6[1-9]|7[0-2]|80)\d{7}$/, other: ['validateCheckDigit'] } }, JP: { BankNumber: { pattern: /^\d{4}$/ }, BranchNumber: { pattern: /^\d{3}$/ } } }; $.fn.bank = function(field, country) { var format; if (formattingRules[country] == null) { throw new Error(country + 'is not supported.'); } if (formattingRules[country][field] == null) { throw new Error('A format of ' + field + ' for ' + country + 'is not supported.'); } format = formattingRules[country][field]; if (format.length != null) { this.on('keypress', restrictLength(format.length)); } if (format.charPattern != null) { return this.on('keypress', restrictCharPattern(format.charPattern)); } }; $.bank.validate = function(field, country, number) { if (validationRules[country] == null) { throw new Error(country + 'is not supported.'); } if (validationRules[country][field] == null) { throw new Error('A validation of ' + field + ' for ' + country + 'is not supported.'); } return validate(validationRules[country][field], number); }; restrictLength = function(length) { return function(e) { var $target; $target = $(e.currentTarget); if (hasTextSelected($target)) { return; } if (!($target.val().length < length)) { return e.preventDefault(); } }; }; restrictCharPattern = function(charPattern) { return function(e) { var char; if (e.metaKey || e.ctrlKey) { return true; } if (e.which === 0) { return true; } if (e.which < 33) { return true; } char = String.fromCharCode(e.which); if (!charPattern.test(char)) { return e.preventDefault(); } }; }; 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; }; validate = function(validation, number) { var name, _i, _len, _ref; number = (number + '').replace(/\s+/g, ''); if (!validation.pattern.test(number)) { return false; } if (validation.other != null) { _ref = validation.other; for (_i = 0, _len = _ref.length; _i < _len; _i++) { name = _ref[_i]; if (!validations[name](number)) { return false; } } } return true; }; validations = { validateCheckDigit: function(number) { var digit, digits, i, sum, _i, _len; digits = number.split(''); sum = 0; for (i = _i = 0, _len = digits.length; _i < _len; i = ++_i) { digit = digits[i]; sum += parseInt(digit) * CHECK_DIGIT_POSITION_WEIGHTS[i % 3]; } return sum !== 0 && sum % 10 === 0; } }; }).call(this);