67 lines
1.9 KiB
JavaScript
Executable File
67 lines
1.9 KiB
JavaScript
Executable File
/**
|
|
* rtn validator
|
|
*
|
|
* @link http://formvalidation.io/validators/rtn/
|
|
* @author https://twitter.com/nghuuphuoc
|
|
* @copyright (c) 2013 - 2015 Nguyen Huu Phuoc
|
|
* @license http://formvalidation.io/license/
|
|
*/
|
|
|
|
(function(root, factory) {
|
|
|
|
"use strict";
|
|
|
|
// AMD module is defined
|
|
if (typeof define === "function" && define.amd) {
|
|
define("validator/rtn", ["jquery", "base"], factory);
|
|
} else {
|
|
// planted over the root!
|
|
factory(root.jQuery, root.FormValidation);
|
|
}
|
|
|
|
}(this, function ($, FormValidation) {
|
|
FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
|
|
'en_US': {
|
|
rtn: {
|
|
'default': 'Please enter a valid RTN number'
|
|
}
|
|
}
|
|
});
|
|
|
|
FormValidation.Validator.rtn = {
|
|
/**
|
|
* Validate a RTN (Routing transit number)
|
|
* Examples:
|
|
* - Valid: 021200025, 789456124
|
|
*
|
|
* @see http://en.wikipedia.org/wiki/Routing_transit_number
|
|
* @param {FormValidation.Base} validator The validator plugin instance
|
|
* @param {jQuery} $field Field element
|
|
* @param {Object} options Can consist of the following keys:
|
|
* - message: The invalid message
|
|
* @returns {Boolean}
|
|
*/
|
|
validate: function(validator, $field, options) {
|
|
var value = validator.getFieldValue($field, 'rtn');
|
|
if (value === '') {
|
|
return true;
|
|
}
|
|
|
|
if (!/^\d{9}$/.test(value)) {
|
|
return false;
|
|
}
|
|
|
|
var sum = 0;
|
|
for (var i = 0; i < value.length; i += 3) {
|
|
sum += parseInt(value.charAt(i), 10) * 3
|
|
+ parseInt(value.charAt(i + 1), 10) * 7
|
|
+ parseInt(value.charAt(i + 2), 10);
|
|
}
|
|
return (sum !== 0 && sum % 10 === 0);
|
|
}
|
|
};
|
|
|
|
|
|
return FormValidation.Validator.rtn;
|
|
}));
|