mirror of
https://github.com/jquery-validation/jquery-validation.git
synced 2025-12-24 12:13:59 +01:00
Split out skip_or_fill_minimum
This commit is contained in:
committed by
Jörn Zaefferer
parent
1ab830383f
commit
c1e54851f9
@@ -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
|
||||
|
||||
35
src/additional/skip_or_fill_minimum.js
Normal file
35
src/additional/skip_or_fill_minimum.js
Normal 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."));
|
||||
Reference in New Issue
Block a user