bootstrapvalidator
Version:
The best jQuery plugin to validate form fields. Designed to use with Bootstrap 3
41 lines (37 loc) • 1.38 kB
JavaScript
(function($) {
$.fn.bootstrapValidator.i18n.sedol = $.extend($.fn.bootstrapValidator.i18n.sedol || {}, {
'default': 'Please enter a valid SEDOL number'
});
$.fn.bootstrapValidator.validators.sedol = {
/**
* Validate a SEDOL (Stock Exchange Daily Official List)
* Examples:
* - Valid: 0263494, B0WNLY7
*
* @see http://en.wikipedia.org/wiki/SEDOL
* @param {BootstrapValidator} 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 = $field.val();
if (value === '') {
return true;
}
value = value.toUpperCase();
if (!/^[0-9A-Z]{7}$/.test(value)) {
return false;
}
var sum = 0,
weight = [1, 3, 1, 7, 3, 9, 1],
length = value.length;
for (var i = 0; i < length - 1; i++) {
sum += weight[i] * parseInt(value.charAt(i), 36);
}
sum = (10 - sum % 10) % 10;
return sum + '' === value.charAt(length - 1);
}
};
}(window.jQuery));