formvalidation
Version:
The best jQuery plugin to validate form fields. Support Bootstrap, Foundation, Pure, SemanticUI, UIKit and custom frameworks
51 lines (47 loc) • 1.61 kB
JavaScript
/**
* 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($) {
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);
}
};
}(jQuery));