谁能告诉我如何编写一条规则来验证用户是否没有选择/填充单选按钮选项或(可选)文本字段?如果没有复选框选项,该规则应该只给出一条消息#myradiogroup
被选择并且文本字段#email2
是空的。
我的表单代码:
<form name="my" id="myForm" action="" method="post">
<input type="radio" name="myradiogroup" id="myradiogroup" value="option 1" /> option 1
<input type="radio" name="myradiogroup" id="myradiogroup" value="option 2" /> option 2
<label for="emailNew4use">this is an optional field:</label>
<input type="text" name="email2" id="email2" />
<input type="submit" value="send">
</form>
The required
jQuery Validate 中的参数可以采用一个函数。
$('#myForm').validate({
rules: {
email2: {
required: function(element) {
if ($('[name="myradiogroup"]:checked').length) {
return false;
} else {
return true;
}
}
},
myradiogroup: {
required: function(element) {
if ($('#email2').val()) {
return false;
} else {
return true;
}
}
}
}
});
这是来自的精简版本Sparky
$('#myForm').validate({
rules: {
email2: {
required: function(element) {
return !$('[name="myradiogroup"]:checked').length;
}
},
myradiogroup: {
required: function(element) {
return !$('#email2').val();
}
}
}
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)