My purpose: If the user field and password field are blank, I want to stop form submitting.
This is my Code that I am trying:
<!DOCTYPE html>
<html>
<head>
<script>
function doit() {
var usr = document.getElementById('ur').value;
var psw = document.getElementById('pw').value;
if ((usr.trim() == '') && (psw.trim() == '')) {
alert(cannot Submit form);
return false;
}
}
</script>
</head>
<body>
<form action=post.php method=post onsubmit=doit()>
User:
<input type=text id=ur name=user>
<br>
<br> Pass:
<input type=password id=pw name=pass>
<br>
<br>
<button>Submit</button>
</form>
</body>
</html>
I am learning JavaScript. Will be helpful if you correct the code with a little explanation why it is not working.