bootstrapvalidator
Version:
The best jQuery plugin to validate form fields. Designed to use with Bootstrap 3
33 lines (29 loc) • 1.19 kB
JavaScript
(function($) {
$.fn.bootstrapValidator.i18n.integer = $.extend($.fn.bootstrapValidator.i18n.integer || {}, {
'default': 'Please enter a valid number'
});
$.fn.bootstrapValidator.validators.integer = {
enableByHtml5: function($field) {
return ('number' === $field.attr('type')) && ($field.attr('step') === undefined || $field.attr('step') % 1 === 0);
},
/**
* Return true if the input value is an integer
*
* @param {BootstrapValidator} validator The validator plugin instance
* @param {jQuery} $field Field element
* @param {Object} options Can consist of the following key:
* - message: The invalid message
* @returns {Boolean}
*/
validate: function(validator, $field, options) {
if (this.enableByHtml5($field) && $field.get(0).validity && $field.get(0).validity.badInput === true) {
return false;
}
var value = $field.val();
if (value === '') {
return true;
}
return /^(?:-?(?:0|[1-9][0-9]*))$/.test(value);
}
};
}(window.jQuery));