I am able to validate my form using javascript, but even if the form data is invalid the next process is done with regards to the submit button. I am unable to stop the page from loading the other page that is supported to be loaded only if there are no errors in my form after javascript validation
script type="text/javascript">
function matchpass(){
var firstpassword=document.myform.psw.value;
var secondpassword=document.myform.pswrepeat.value;
var x=document.myform.email.value;
var atposition=x.indexOf("@");
var dotposition=x.lastIndexOf(".");
if (atposition<1 || dotposition<atposition+2 || dotposition+2>=x.length)
{
alert("Please enter a valid e-mail address \n atpostion:"+atposition+"\n dotposition:"+dotposition);
return false;
}
if(firstpassword==secondpassword)
{
return true;
}
else
{
alert("password must be same!");
return false;
}
}
</script>
</head>
<body>
</div>
<form name="myform" action="RegisterControllerServlet" method="post" >
<input type="hidden" name="command" value="ADD">
<div class="container">
<p>Please fill in this form to create an account.</p>
<hr>
<label for="email"><b>Enter your Employee id</b></label>
<input type="text" name="de_eid" >
<label for="email"><b>Email</b></label>
<input type="text" name="email">
<Select name="is" class="custom-select" style="width:500px; background-color: #A0A0A0; height:40px;">
<option value="">Choose Occupation</option>
<option value="Doctor">Doctor</option>
<option value="Receptionist">Receptionist</option>
<option value="labassistant">Lab assistant</option>
<option value="pharmacist">Pharmacist</option>
</Select>
<br><br>
<label for="psw"><b>Password</b></label>
<input type="password" placeholder="Enter Password" name="psw" required>
<label for="psw-repeat"><b>Repeat Password</b></label>
<input type="password" placeholder="Repeat Password" name="pswrepeat" required>
<hr>
<button type="submit" class="registerbtn" onclick="matchpass();">Register</button>
</div>
I want the page not to redirect the user to the page that is supposed to be loaded only if there are no errors in validation