bootstrapvalidator
Version:
The best jQuery plugin to validate form fields. Designed to use with Bootstrap 3
34 lines (30 loc) • 1.28 kB
JavaScript
(function($) {
$.fn.bootstrapValidator.i18n.hexColor = $.extend($.fn.bootstrapValidator.i18n.hexColor || {}, {
'default': 'Please enter a valid hex color'
});
$.fn.bootstrapValidator.validators.hexColor = {
enableByHtml5: function($field) {
return ('color' === $field.attr('type'));
},
/**
* Return true if the input value is a valid hex color
*
* @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;
}
return ('color' === $field.attr('type'))
// Only accept 6 hex character values due to the HTML 5 spec
// See http://www.w3.org/TR/html-markup/input.color.html#input.color.attrs.value
? /^#[0-9A-F]{6}$/i.test(value)
: /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(value);
}
};
}(window.jQuery));