I'm having trouble getting the focus to return to the input box in my form after an invalid entry triggers an alert box. I've written what should be the correct code, but for some reason, it's not working as expected.
Here's the code snippet I've been using:
HTML:
<form name="myform" onsubmit="validate()">
Amount <input name="num" id="principal"gt;<br/> </form>
JS:
function validate(){
var num = document.myform.num.value;
if (isNaN(num) || num == 0 || num < 0 || num == null) {
alert("Please enter a valid entry")
document.getElementsById("principal").focus(); }
else {
return true;
}
}
I appreciate any help in identifying where I may have gone wrong. Thank you!