I am trying to give validation to both fields email and name but only email is getting validated and its not checking for name field. I am not able to find what is wrong in the code.
<html>
<body>
<form name="myForm">
Email: <input type="text" name="email" id="email2">
<br>
Name: <input type="text" name=" fname">
<button type="submit" name="submit" onclick="validateForm()">submit</button>
<br> email
<div id="email1"></div>
</form>
</body>
<script>
function validateForm() {
var x = document.forms["myForm"]["email"].value;
var atpos = x.indexOf("@");
var dotpos = x.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) {
alert("Not a valid e-mail address");
return false;
}
var email1 = document.getElementById("email2").value;
document.getElementById("email1").innerHTML=email1;
var y = document.forms["myForm"]["fname"].value;
if (y==null ||y=="") {
alert("name must be filled");
return false;
}
}
</script>
</html>
return false;will return the control and the code below will not be executed.<input type="email" name="email" required />