Split out skip_or_fill_minimum

This commit is contained in:
Nick Schonning
2013-03-22 20:33:59 -04:00
committed by Jörn Zaefferer
parent 1ab830383f
commit c1e54851f9
2 changed files with 35 additions and 36 deletions

View File

@@ -33,42 +33,6 @@
}());
/*
* Lets you say "either at least X inputs that match selector Y must be filled,
* OR they must all be skipped (left blank)."
*
* The end result, is that none of these inputs:
*
* <input class="productinfo" name="partnumber">
* <input class="productinfo" name="description">
* <input class="productinfo" name="color">
*
* ...will validate unless either at least two of them are filled,
* OR none of them are.
*
* partnumber: {skip_or_fill_minimum: [2,".productinfo"]},
* description: {skip_or_fill_minimum: [2,".productinfo"]},
* color: {skip_or_fill_minimum: [2,".productinfo"]}
*
*/
jQuery.validator.addMethod("skip_or_fill_minimum", function(value, element, options) {
var validator = this,
numberRequired = options[0],
selector = options[1];
var numberFilled = jQuery(selector, element.form).filter(function() {
return validator.elementValue(this);
}).length;
var valid = numberFilled >= numberRequired || numberFilled === 0;
if(!jQuery(element).data('being_validated')) {
var fields = $(selector, element.form);
fields.data('being_validated', true);
fields.valid();
fields.data('being_validated', false);
}
return valid;
}, jQuery.format("Please either skip these fields or fill at least {0} of them."));
// Accept a value from a file input based on a required mimetype
jQuery.validator.addMethod("accept", function(value, element, param) {
// Split mime on commas in case we have multiple types we can accept

View File

@@ -0,0 +1,35 @@
/*
* Lets you say "either at least X inputs that match selector Y must be filled,
* OR they must all be skipped (left blank)."
*
* The end result, is that none of these inputs:
*
* <input class="productinfo" name="partnumber">
* <input class="productinfo" name="description">
* <input class="productinfo" name="color">
*
* ...will validate unless either at least two of them are filled,
* OR none of them are.
*
* partnumber: {skip_or_fill_minimum: [2,".productinfo"]},
* description: {skip_or_fill_minimum: [2,".productinfo"]},
* color: {skip_or_fill_minimum: [2,".productinfo"]}
*
*/
jQuery.validator.addMethod("skip_or_fill_minimum", function(value, element, options) {
var validator = this,
numberRequired = options[0],
selector = options[1];
var numberFilled = jQuery(selector, element.form).filter(function() {
return validator.elementValue(this);
}).length;
var valid = numberFilled >= numberRequired || numberFilled === 0;
if(!jQuery(element).data('being_validated')) {
var fields = $(selector, element.form);
fields.data('being_validated', true);
fields.valid();
fields.data('being_validated', false);
}
return valid;
}, jQuery.format("Please either skip these fields or fill at least {0} of them."));