formvalidation
Version:
The best jQuery plugin to validate form fields. Support Bootstrap, Foundation, Pure, SemanticUI, UIKit and custom frameworks
83 lines (74 loc) • 3.44 kB
JavaScript
/**
* choice validator
*
* @link http://formvalidation.io/validators/choice/
* @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': {
choice: {
'default': 'Please enter a valid value',
less: 'Please choose %s options at minimum',
more: 'Please choose %s options at maximum',
between: 'Please choose %s - %s options'
}
}
});
FormValidation.Validator.choice = {
html5Attributes: {
message: 'message',
min: 'min',
max: 'max'
},
/**
* Check if the number of checked boxes are less or more than a given number
*
* @param {FormValidation.Base} validator The validator plugin instance
* @param {jQuery} $field Field element
* @param {Object} options Consists of following keys:
* - min
* - max
*
* At least one of two keys is required
* The min, max keys define the number which the field value compares to. min, max can be
* - A number
* - Name of field which its value defines the number
* - Name of callback function that returns the number
* - A callback function that returns the number
*
* - message: The invalid message
* @returns {Object}
*/
validate: function(validator, $field, options) {
var locale = validator.getLocale(),
ns = validator.getNamespace(),
numChoices = $field.is('select')
? validator.getFieldElements($field.attr('data-' + ns + '-field')).find('option').filter(':selected').length
: validator.getFieldElements($field.attr('data-' + ns + '-field')).filter(':checked').length,
min = options.min ? ($.isNumeric(options.min) ? options.min : validator.getDynamicOption($field, options.min)) : null,
max = options.max ? ($.isNumeric(options.max) ? options.max : validator.getDynamicOption($field, options.max)) : null,
isValid = true,
message = options.message || FormValidation.I18n[locale].choice['default'];
if ((min && numChoices < parseInt(min, 10)) || (max && numChoices > parseInt(max, 10))) {
isValid = false;
}
switch (true) {
case (!!min && !!max):
message = FormValidation.Helper.format(options.message || FormValidation.I18n[locale].choice.between, [parseInt(min, 10), parseInt(max, 10)]);
break;
case (!!min):
message = FormValidation.Helper.format(options.message || FormValidation.I18n[locale].choice.less, parseInt(min, 10));
break;
case (!!max):
message = FormValidation.Helper.format(options.message || FormValidation.I18n[locale].choice.more, parseInt(max, 10));
break;
default:
break;
}
return { valid: isValid, message: message };
}
};
}(jQuery));