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

JavaScript Registration Form Validation

<html>
<head>

<script>
function validateForm() {
var fname = document.getElementById('fname').value;
var lname = document.getElementById('lname').value;
var email = document.getElementById('email').value;
var password = document.getElementById('password').value;
var confirm_password = document.getElementById('confirm_password').value;

if (fname == '') {
alert('First name is required.');
return false;
}

if (lname == '') {
alert('Last name is required.');
return false;
}

if (email == '') {
alert('Email is required.');
return false;
}

if (password == '') {
alert('Password is required.');
return false;
}

if (confirm_password == '') {
alert('Confirm password is required.');
return false;
}

if (password != confirm_password) {
alert('Passwords do not match.');
return false;
}

return true;
}
</script>
</head>
<title>Registration Form</title>
<body>

<h1>Registration Form</h1>

<form action="action.html" method="post">


<label for="fname">First name:</label>
<input type="text" id="fname" name="fname" required>
<br>
<label for="lname">Last name:</label>
<input type="text" id="lname" name="lname" required>
<br>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
<br>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required>
<br>
<label for="confirm_password">Confirm password:</label>
<input type="password" id="confirm_password" name="confirm_password" required>
<br>
<input type="submit" value="Submit" onsubmit="return validateForm()">
</form>
</body>
</html>

You might also like