<div> 容器中的 Jquery 表单验证自定义错误位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4775415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery form validation custom error placement in <div> container
提问by blasteralfred Ψ
I am using jquery validate plugin to validate my form fields.
我正在使用 jquery 验证插件来验证我的表单字段。
The script is like below
脚本如下
jQuery(document).ready(function($){
$.validator.addMethod(
"mydate",
function(value, element) {
return value.match(/^\d\d?\-\d\d?\-\d\d\d\d$/);
},
"Please enter a date in the format dd-mm-yyyy"
);
var validator = $("#signupform").validate({
rules: {
User: {
required: true,
remote: {
url: "user.php",
type: "post",
},
},
"RID[]": {
required: true,
remote: {
url: "resource.php",
type: "post",
},
},
Date: {
required: true,
mydate : true
},
},
messages: {
User: "Specify Active User",
"RID[]": "Specify Available Resource",
Date: "Specify Date"
},
errorPlacement: function(error, element) {
error.appendTo( element.parent().next() );
},
success: function(label) {
label.html("OK").addClass("checked");
}
});
which validates one of my form field
这验证了我的表单字段之一
<tr>
<td style="width: 70px" class="style22" valign="top">Resource ID</td>
<td id="resource" style="width: 267px;">
<input id="resource" name="RID[]" type="text" value="" style="width: 232px" /></td>
<td class="style21" style="width: 160px" valign="top">
<img id="addScnt" alt="[+]" src="+.gif"/>
</td>
<td>
</td>
</tr>
But I want to place error for "RID[]" in a specific div <div id="errorcontainer2">
.
但我想在特定的 div 中放置“RID[]”的错误<div id="errorcontainer2">
。
How can I make this possible??
我怎样才能做到这一点?
Thanks in advance.. :)
提前致谢.. :)
blasteralfred
布拉拉弗雷德
回答by blasteralfred Ψ
I solved it using custom error placement in a specific div <div id="errorbox">
我使用特定 div 中的自定义错误位置解决了它 <div id="errorbox">
errorPlacement: function(error, element){
if(element.attr("name") == "RID[]"){
error.appendTo($('#errorbox'));
}else{
error.appendTo( element.parent().next() );
}
}
回答by nrobey
回答by hkansal
Use the following structure and logic in errorPlacement
option of validator to code:
在errorPlacement
验证器选项中使用以下结构和逻辑进行编码:
errorPlacement: function(error, element){
if("RID[]" == element.attr("name")){
// Place error for RID[] as you want
}else{
// Place errors normally
}
}