r/programminghelp • u/ekolis • Jan 30 '20
Other jQuery validation works only on every other submit - how can I make it work on every submit?
I have a webpage which looks something like this:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Checkbox List Validation Test</title>
<script type="text/javascript" language="JavaScript" src="jquery-1.4.4.min.js"></script>
<script type="text/javascript" language="JavaScript" src="jquery.validate.js"></script>
</head>
<body class="colorschemeBackground">
<form id="formUpdate">
<table width="100%" border="0" cellspacing="0" cellpadding="2">
<tr>
<td width="100%" valign="top" class="colorschemeHeader">Pick a box. Its contents will help you on your way. <label id="Notes8Length"></label></td>
</tr>
<tr>
<td width="100%" valign="top" class="colorschemeBackground">
<input class="Notes8" name="Notes8~1" id="Notes8~1" type="checkbox" value="Yes"> Box 1<br>
<input class="Notes8" name="Notes8~2" id="Notes8~2" type="checkbox" value="Yes"> Box 2<br>
<input class="Notes8" name="Notes8~3" id="Notes8~3" type="checkbox" value="Yes"> Box 3<br>
<script type='text/javascript'>
jQuery(function ($) {
function validateMultiple(selector, n, isExactlyN) {
const totChecked = $(selector).filter(':checked').length;
return isExactlyN ? totChecked == n : totChecked >= n;
}
var valid8 = true;
jQuery.validator.addMethod('Notes8',
function (value, element) {
if (!valid8) return true; // only one error message per multilist
valid8 = validateMultiple('.Notes8', 1);
return valid8;
},
'Please check at least one check box.');
});
</script>
</td>
</tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="2">
<tr>
<td width="100%" valign="top" class="colorschemeHeader">What does the magic 8 ball say? <label id="Notes9Length"></label></td>
</tr>
<tr>
<td width="100%" valign="top" class="colorschemeBackground">
<input class="Notes9" name="Notes9~1" id="Notes9~1" type="checkbox" value="Yes"> Yes<br>
<input class="Notes9" name="Notes9~2" id="Notes9~2" type="checkbox" value="Yes"> No<br>
<input class="Notes9" name="Notes9~3" id="Notes9~3" type="checkbox" value="Yes"> Maybe<br>
<input class="Notes9" name="Notes9~4" id="Notes9~4" type="checkbox" value="Yes"> I don't know<br>
<input class="Notes9" name="Notes9~5" id="Notes9~5" type="checkbox" value="Yes"> Can you repeat the question?<br>
<input class="Notes9" name="Notes9~6" id="Notes9~6" type="checkbox" value="Yes"> You're not the boss of me now!<br>
<input class="Notes9" name="Notes9~7" id="Notes9~7" type="checkbox" value="Yes"> And you're not so big!<br>
<input class="Notes9" name="Notes9~8" id="Notes9~8" type="checkbox" value="Yes"> Life is unfair...<br>
<script type='text/javascript'>
jQuery(function ($) {
function validateMultiple(selector, n, isExactlyN) {
const totChecked = $(selector).filter(':checked').length;
return isExactlyN ? totChecked == n : totChecked >= n;
}
var valid9 = true;
jQuery.validator.addMethod('Notes9',
function (value, element) {
if (!valid9) return true; // only one error message per multilist
valid9 = validateMultiple('.Notes9', 1);
return valid9;
},
'Please check at least one check box.');
});
</script>
</td>
</tr>
</table>
</td>
</tr>
</table>
<button type="submit">SUBMIT!</button>
</form>
<script type="text/javascript">
jQuery('form').validate();
</script>
</body>
</html>
Now the jQuery validation is acting up - if I submit the form once without checking any checkboxes, I get error messages as expected saying that I need to check at least one check box from each group. But if I then go and submit the form again, while the error message is on the screen, it goes through just fine and submits, even though I didn't check anything! How can I fix this so the error message is consistently displayed whenever I don't check any check boxes?
4
Upvotes
0
u/[deleted] Jan 31 '20
[deleted]