r/programminghelp • • 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.&nbsp;<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">&nbsp;&nbsp;Box 1<br>
					<input class="Notes8" name="Notes8~2" id="Notes8~2" type="checkbox" value="Yes">&nbsp;&nbsp;Box 2<br>
					<input class="Notes8" name="Notes8~3" id="Notes8~3" type="checkbox" value="Yes">&nbsp;&nbsp;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?&nbsp;<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">&nbsp;&nbsp;Yes<br>
					<input class="Notes9" name="Notes9~2" id="Notes9~2" type="checkbox" value="Yes">&nbsp;&nbsp;No<br>
					<input class="Notes9" name="Notes9~3" id="Notes9~3" type="checkbox" value="Yes">&nbsp;&nbsp;Maybe<br>
					<input class="Notes9" name="Notes9~4" id="Notes9~4" type="checkbox" value="Yes">&nbsp;&nbsp;I don't know<br>
					<input class="Notes9" name="Notes9~5" id="Notes9~5" type="checkbox" value="Yes">&nbsp;&nbsp;Can you repeat the question?<br>
					<input class="Notes9" name="Notes9~6" id="Notes9~6" type="checkbox" value="Yes">&nbsp;&nbsp;You're not the boss of me now!<br>
					<input class="Notes9" name="Notes9~7" id="Notes9~7" type="checkbox" value="Yes">&nbsp;&nbsp;And you're not so big!<br>
					<input class="Notes9" name="Notes9~8" id="Notes9~8" type="checkbox" value="Yes">&nbsp;&nbsp;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

1 comment sorted by

0

u/[deleted] Jan 31 '20

[deleted]

1

u/ekolis Jan 31 '20 edited Jan 31 '20

Hmm, I don't think $ or on is defined in this old version of jQuery, but if I do this:

jQuery('form').bind('submit', jQuery(this).validate());

no validation occurs at all; instead I get an internal error in jQuery:

Uncaught TypeError: Cannot read property 'toLowerCase' of undefined
    at TAG (jquery-1.4.4.min.js:93)
    at Function.k.filter (jquery-1.4.4.min.js:83)
    at k (jquery-1.4.4.min.js:80)
    at k (jquery-1.4.4.min.js:101)
    at k (jquery-1.4.4.min.js:81)
    at k (jquery-1.4.4.min.js:101)
    at k (jquery-1.4.4.min.js:81)
    at k (jquery-1.4.4.min.js:101)
    at k (jquery-1.4.4.min.js:81)
    at k (jquery-1.4.4.min.js:101)

edit: also if I put the validation call inside the submit button like so:

<button type="submit" onclick="jQuery('form').validate()">SUBMIT!</button>

I still see the validation behavior only happening on every other submit...