问题
I am using require _from_group i n my from for validation but it is not working.
My script code is HERE:
jQuery.validator.setDefaults({ debug: true, $(document).ready(function () {
jQuery.validator.setDefaults({
debug: true,
success: "valid"
});
$("#findproject_form").validate({
rules: {
Profession: {
require_from_group: [1, ".validategroup"]
},
Location1: {
require_from_group: [1, ".validategroup"]
},
Company: {
require_from_group: [1, ".validategroup"]
},
Name: {
require_from_group: [1, ".validategroup"]
},
KeyWord: {
require_from_group: [1, ".validategroup"]
}
}
});
$.validator.addMethod("require_from_group", function (value, element, options) {
var $fields = $(options[1], element.form),
$fieldsFirst = $fields.eq(0),
validator = $fieldsFirst.data("valid_req_grp") ? $fieldsFirst.data("valid_req_grp") : $.extend({}, this),
isValid = $fields.filter(function () {
return validator.elementValue(this);
}).length >= options[0];
// Store the cloned validator for future validation
$fieldsFirst.data("valid_req_grp", validator);
// If element isn't being validated, run each require_from_group field's validation rules
if (!$(element).data("being_validated")) {
$fields.data("being_validated", true);
$fields.each(function () {
validator.element(this);
});
$fields.data("being_validated", false);
}
return isValid;
}, $.validator.format("Please fill at least {0} of these fields."));
});
It gives error when any one or more field are fill_out instead of when it is empty and not goto the action page. So what is wrong in that.
MY FORM IS HERE
<form id="findproject_form" method="post" action="{$BASE_URL}findproject" >
<div class="bgtrans"><h3>Search By</h3>
<div class="div_bg1">
<div class="searchbg"><div class="seachbginputbg"><input class="seachbginput validategroup" type="text" placeholder="Profession" id="Profession" name="Profession" value="{if $profession}{$profession|escape}{/if}" /></div></div>
<div class="searchbg"><div class="seachbginputbg"><input class="seachbginput validategroup" type="text" placeholder="Location" id="Location1" name="Location1" value="{if $location}{$location|escape}{/if}" /></div></div>
<div class="searchbg" style="margin-right:0px;">
<div class="seachbginputbg"><input class="seachbginput validategroup" type="text" placeholder="Company" id="Company" name="Company" value="{if $company}{$company|escape}{/if}"/></div>
</div>
<div class="clear"></div>
</div>
<div class="div_bg2">
<div class="searchbg"><div class="seachbginputbg"><input class="seachbginput validategroup" type="text" placeholder="Name" id="Name" name="Name" value="{if $name}{$name|escape}{/if}" /></div></div>
<div class="searchbg"><div class="seachbginputbg"><input class="seachbginput validategroup" type="text" placeholder="Key Words" id="KeyWord" name="KeyWord" value="{if $keyword}{$keyword|escape}{/if}"/></div></div>
<div class="searchbg" style="margin-right:0px;">
<input class="bgbttn" type="submit" name="submit" value="" />
</div>
<div class="clear"></div>
</div>
</div>
回答1:
Remove your custom method. (It makes the field optional and it's comparing the value of your field to the sum of your parameters. Since your parameters are the number 1 and a class name, clearly, this makes no sense.)...
jQuery.validator.addMethod("require_from_group", function(value, element, params) {
return this.optional(element) || value == params[0] + params[1];
}, jQuery.validator.format("Please fill out atleast one field "));
That does not make sense because that's not really the require_from_group function... that's just some sample code from the .addMethod() documentation with the name changed into "require_from_group".
Use the default version of require_from_group method/rule included within the additional-methods.js file. You can include the whole file or copy the one method below.
As you can see, this is not a trivial function.
$.validator.addMethod("require_from_group", function(value, element, options) {
var $fields = $(options[1], element.form),
$fieldsFirst = $fields.eq(0),
validator = $fieldsFirst.data("valid_req_grp") ? $fieldsFirst.data("valid_req_grp") : $.extend({}, this),
isValid = $fields.filter(function() {
return validator.elementValue(this);
}).length >= options[0];
// Store the cloned validator for future validation
$fieldsFirst.data("valid_req_grp", validator);
// If element isn't being validated, run each require_from_group field's validation rules
if (!$(element).data("being_validated")) {
$fields.data("being_validated", true);
$fields.each(function() {
validator.element(this);
});
$fields.data("being_validated", false);
}
return isValid;
}, $.validator.format("Please fill at least {0} of these fields."));
DEMO (with your jQuery and HTML): http://jsfiddle.net/y3qayufu/2/
来源:https://stackoverflow.com/questions/27271606/jquery-validate-require-from-group-is-not-working