I know I've been asking about jQuery validation a lot lately, but I think I have my problem boiled down to the bare essentials now.
So if I have this HTML page:
<html>
<head>
<title>validation test</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.validate.js"></script>
</head>
<body>
<form id="theform">
<div id="num" name="num">86</div>
</form>
<script type="text/javascript">
var validationObject = {
rules: {
num: {
numtest:true
}
}
};
$.validator.addMethod("numtest", function(value, element) {
return value == 42;
});
$('form').validate(validationObject);
if ($("form").valid())
alert("VALID");
else
alert ("INVALID");
</script>
</body>
</html>
Then I expect the alert to be "INVALID" because num is 86 instead of 42. But instead I get "VALID"; the function I defined to do the validation is not being called (I set a breakpoint on return value == 42; and it never gets hit). Why is this? How can I make sure the validation function gets called?
divwhich isn't a form elementinput type='text'fixes it: jsfiddle.net/c38uxh6m