Forgive me if this is overly simplistic, but as someone new to Javascript and not particularly skilled at coding, I'm struggling with validating two basic forms using a single submit button. If anyone could offer some guidance on how to tackle this, I would greatly appreciate it! Below is what I have so far:
function validateName() {
var name = document.forms["Name"]["fname"].value;
if (name == null || name == "") {
alert("Please enter your first name");
return false;
}
}
function validateEmail() {
var email = document.forms["Mail"]["email"].value;
var atpos = email.indexOf("@");
var dotpos = email.lastIndexOf(".");
if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= email.length) {
alert("Please enter a valid email address");
return false;
}
}
<style type="text/css">
</style>
<link rel="stylesheet" href="teststyle.css" type="text/css">
</head>
<body>
<form name="Name" action="MyForm.html" onsubmit="return validateName()" method="post">
First Name: <input name="fname" type="text"> <br>
<br>
Email: <input name="Mail" type="text">
</form>
<br>
<input value="Submit" type="submit"><br>
<br>
</body>