Experiment 2: Use JavaScript to validate forms
This experiment demonstrates how to implement client-side form validation using JavaScript.
Overview
In this experiment, we enhanced a basic login form with JavaScript validation to:
- Validate email format using regular expressions
- Ensure password meets minimum length requirements
- Provide immediate feedback to users
- Prevent form submission if validation fails
Implementation
The implementation consists of three main files:
index.html: Contains the HTML structure of the login formstyles.css: Contains all the styling for the login pagescript.js: Contains the JavaScript validation logic
JavaScript Code (script.js)
document.addEventListener("DOMContentLoaded", function () {
const form = document.querySelector("form");
form.addEventListener("submit", function (event) {
event.preventDefault();
const email = form.email.value.trim();
const password = form.password.value.trim();
let isValid = true;
if (email === "") {
alert("Email is required.");
isValid = false;
}
else if (!validateEmail(email)) {
alert("Please enter a valid email address.");
isValid = false;
}
if (password === "") {
alert("Password is required.");
isValid = false;
}
else if (password.length < 6) {
alert("Password must be at least 6 characters long.");
isValid = false;
}
if (isValid) {
alert("Login successful!");
form.submit();
}
});
function validateEmail(email) {
const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailPattern.test(email);
}
});
HTML Code (index.html)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login Page</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="login-container">
<h2>Login</h2>
<form>
<div class="form-group">
<label for="email">Email</label>
<input type="email" name="email" required >
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" id="password" name="password" required>
</div>
<button type="submit" class="login-button">Login</button>
</form>
</div>
<script src="script.js"></script>
</body>
</html>
CSS Code (styles.css)
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.login-container {
background: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
width: 300px;
}
.login-container h2 {
margin-bottom: 20px;
text-align: center;
}
.form-group {
margin-bottom: 15px;
}
.form-group label {
display: block;
font-size: 14px;
margin-bottom: 5px;
}
.form-group input {
width: 100%;
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
.login-button {
width: 100%;
padding: 10px;
background-color: #007BFF;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
.login-button:hover {
background-color: #0056b3;
}
Preview
The form looks similar to Experiment 1 but includes validation feedback when users attempt to submit invalid data.
View Implementation
👉 See the live implementation of this form validation example.