admin管理员组文章数量:1386636
Can anyone tell me how to write a rule that validates if neither one radio button option nor the (optinal) textfield is chosen/filled by a user? The rule should only give a message if no checkbox option #myradiogroup
is chosen AND the textfield #email2
is empty.
My form code:
<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>
Can anyone tell me how to write a rule that validates if neither one radio button option nor the (optinal) textfield is chosen/filled by a user? The rule should only give a message if no checkbox option #myradiogroup
is chosen AND the textfield #email2
is empty.
My form code:
<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>
Share
Improve this question
edited Mar 5, 2014 at 0:13
apaul
16.2k8 gold badges49 silver badges82 bronze badges
asked Sep 19, 2013 at 14:39
michbeckmichbeck
7453 gold badges11 silver badges17 bronze badges
2
- it would have been really helpful if you had been much clearer in what you were expecting as an answer – hima Commented Sep 19, 2013 at 15:42
-
1
You should also show your
.validate()
code. – Sparky Commented Sep 19, 2013 at 15:42
2 Answers
Reset to default 5The required
parameter in jQuery Validate can take a function.
$('#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;
}
}
}
}
});
Here's a condensed version from Sparky
$('#myForm').validate({
rules: {
email2: {
required: function(element) {
return !$('[name="myradiogroup"]:checked').length;
}
},
myradiogroup: {
required: function(element) {
return !$('#email2').val();
}
}
}
});
maybe can this:
$('#myForm').submit(function() {
if($('input:radio').val() == '' || $('#email2').val() == '') {
preventDefault();
// show error
}
}
本文标签: javascriptconditional form validation using jqueryvalidatejs pluginStack Overflow
版权声明:本文标题:javascript - conditional form validation using jquery.validate.js plugin - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744520026a2610398.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论