我使用的是http://bassistance.de/的jQuery验证插件1.9.0。问题是,只有当用户在文本字段中键入内容时,我才想不出如何验证文本字段。如果用户未编写任何内容,则不应将该字段视为必填字段。
发布于 2011-10-27 17:10:13
不要设置required规则选项-
rules : {
text1 : {
required: false, // Or do not specify the option
minlength: 3 // validation if it has value
}
},发布于 2011-10-27 17:06:29
if ( value != '' ) {
// Do your validation here
}发布于 2011-10-28 00:24:27
您可以将函数传递给验证规则。例如
$("#demoForm").validate({
rules: {
"requiredField": {sampleRule: function(e){
var input = $(e);
if(input.val())
return true;
return false;
}
}
},
messages : {
"requiredField": "This field is invalid"
}其中"sampleRule“可以是你想要的任何规则,例如。“必需”或"minlength“。该函数提供了添加条件规则的功能。
https://stackoverflow.com/questions/7913769
复制相似问题