jQuery验证require_from_group无法正常工作 [英] jQuery validate require_from_group is not working
问题描述
我正在使用来自from的require _from_group进行验证,但无法正常工作.
I am using require _from_group i n my from for validation but it is not working.
我的脚本代码在这里:
jQuery.validator.setDefaults({ 调试:是的, $(document).ready(function(){
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."));
});
当任何一个或多个字段为fill_out而不是为空并且不转到操作页面时,它将给出错误. 那么那是怎么回事.
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.
我的表格在这里
<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 "));
这没有意义,因为这实际上不是require_from_group
函数... 那只是一些示例.addMethod()
文档中的代码,名称更改为"require_from_group"
.
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."));
演示(使用jQuery和HTML): http://jsfiddle.net/y3qayufu/2 /
DEMO (with your jQuery and HTML): http://jsfiddle.net/y3qayufu/2/
这篇关于jQuery验证require_from_group无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!