sto*_*oic 7 jquery jquery-validate
我正在使用流行的Bassistance jquery验证插件来验证我的表单.在同一个表单上,我正在使用xoxco的jQuery Tags Input Plugin.
我能够验证表单上的所有表单字段,除了"标签输入"插件正在使用的表单字段.
原因是,原始输入被隐藏,插件绘制了新的输入.
任何帮助将我的验证样式应用于标签输入将是赞赏,Thanx
两个问题:
name.如果属性不存在,验证将无法正常运行.ignore: []包含隐藏字段的验证.总而言之,您的代码应如下所示:
HTML:
<form id="someForm" name="someForm" method="post">
<input type="text" name="required-field" class="required"/>
<input id="tagsx" type="text" name="tags" class="required" />
<input type="submit" value="submit"/>
</form>
Run Code Online (Sandbox Code Playgroud)
JavaScript的:
$(document).ready(function () {
$("#someForm").validate({
ignore: []
});
$('#tagsx').tagsInput({
width: 'auto',
autocomplete_url: 'http://xoxco.com/projects/code/tagsinput/test/fake_json_endpoint.html'
});
});
Run Code Online (Sandbox Code Playgroud)
示例: http ://jsfiddle.net/5eC5B/
请注意,您必须使用errorPlacement或类似才能正确显示错误消息.
小智 8
您可以通过使用自定义方法验证虚拟文本字段来验证标记字段.
<form id="someForm" name="someForm" method="post">
<input type="text" id="txt1" name="txt1"/>
<input id="tagsx" name="tagsx" type="text" />
<input id="dummy" name="dummy" type="text" /><!-- dummy text field -->
<input type="submit" value="submit"/>
</form>
Run Code Online (Sandbox Code Playgroud)
/*To hide dummy text field*/
#dummy{
visibility: hidden!Important;
height:1px;
width:1px;
}
Run Code Online (Sandbox Code Playgroud)
$(document).ready(function () {
$.validator.addMethod("checkTags", function(value) { //add custom method
//Tags input plugin converts input into div having id #YOURINPUTID_tagsinput
//now you can count no of tags
return ($("#tagsx_tagsinput").find(".tag").length > 0);
});
$("#someForm").validate( {
rules: {
txt1:"required",
dummy:"checkTags"
},
messages: {
txt1: "Required",
dummy: "Required"
}
});
$('#tagsx').tagsInput({
width: 'auto',
autocomplete_url: 'http://xoxco.com/projects/code/tagsinput/test/fake_json_endpoint.html'
});
});
Run Code Online (Sandbox Code Playgroud)
验证插件的默认设置ignore: ":hidden"用于jQuery.not()从忽略选项中排除所有内容。
如果你想包含隐藏字段,你可以使用这个:
$("#myform").validate({
ignore: ""
});
Run Code Online (Sandbox Code Playgroud)
在您的情况下,请使用此 Javascript 代码:
$(document).ready(function () {
$("#someForm").validate({
ignore: ""
});
$('#tagsx').tagsInput({
width: 'auto',
autocomplete_url: 'http://xoxco.com/projects/code/tagsinput/test/fake_json_endpoint.html'
});
});
Run Code Online (Sandbox Code Playgroud)
更新:正如我在验证代码中看到的那样,name强烈需要 -Attribute,因为插件以name. 即使您只添加ignore: ""到您的代码中,它也会给出一个验证错误,因为它以相同name的名称存储并且名称是null.
这是一个有效的 JSFiddle:
| 归档时间: |
|
| 查看次数: |
9913 次 |
| 最近记录: |