I have made a short demo of a javascript problem. When the Submit button is clicked, the alert pops up informing the user that a blank value is not allowed, which is good. But then the page submits anyways, which is bad.
It seems this behavior is only happending on hidden fields.
(Its more clear if you enter any value into the name field. Click Submit. I know the page submits because the value in Name clears out.)
Can anyone see what is wrong here?
<html>
<body>
<form action="" method="post" name="form" onsubmit='return validate()'>
Name: <input type="text" name="name" id="name">
<br><br>Signature: Hidden <input type="hidden" name="poa_esign_signature" id="poa_esign_signature">
<br><br><button type="submit">Sign</button>
</form>
<script type="text/javascript">
function validate() {
var element=document.getElementById('poa_esign_signature');
if (element.value=='') {
alert('Signature may not be blank.');
element.focus();
return false;
}
}
</script>
</body>
</html>
Check your console. I bet the
element.focus()is failing, because its a hidden input. Seems odd to focus on a hidden element.