Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

<!

DOCTYPE html>
<html>
<head>
<title>Form Validation</title>
<script>
function validateForm() {
var name = document.forms["myForm"]["name"].value;
var email = document.forms["myForm"]["email"].value;
var password = document.forms["myForm"]["password"].value;

// Check if name field is empty


if (name == "") {
alert("Name must be filled out");
return false;
}

// Check if email field is empty


if (email == "") {
alert("Email must be filled out");
return false;
}

// Check if email is in valid format using a simple regex


var emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailRegex.test(email)) {
alert("Please enter a valid email address");
return false;
}

// Check if password field is empty


if (password == "") {
alert("Password must be filled out");
return false;
}

// Check if password is at least 6 characters long


if (password.length < 6) {
alert("Password must be at least 6 characters long");
return false;
}

// If all validations pass, return true to submit the form


return true;
}
</script>
</head>
<body>

<h2>Form Validation</h2>

<form name="myForm" onsubmit="return validateForm()" method="post">


<label for="name">Name:</label>
<input type="text" id="name" name="name"><br><br>

<label for="email">Email:</label>
<input type="text" id="email" name="email"><br><br>

<label for="password">Password:</label>
<input type="password" id="password" name="password"><br><br>
<input type="submit" value="Submit">
</form>

</body>
</html>

You might also like