Table of Contents
In this project, you will build a sign-up form with input validation.
The form shows inline error messages under each field and prevents submission until all fields are filled and valid.
Start by creating the basic structure containing four text fields (Name, Email, Password, Confirm Password), associated error message paragraphs, and a submit button.
<!DOCTYPE html>
<html>
<body>
<h2>Sign Up</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<form id="signupForm">
<div class="field">
<label>Name:</label><br>
<input id="name" type="text" placeholder="Your name">
<p id="nameError" class="error"></p>
</div>
<div class="field">
<label>Email:</label><br>
<input id="email" type="text" placeholder="name@example.com">
<p id="emailError" class="error"></p>
</div>
<div class="field">
<label>Password:</label><br>
<input id="password" type="password" placeholder="Min 8 characters">
<p id="passwordError" class="error"></p>
</div>
<div class="field">
<label>Confirm Password:</label><br>
<input id="confirm" type="password" placeholder="Repeat password">
<p id="confirmError" class="error"></p>
</div>
<p><button type="submit">Create Account</button></p>
</form>
<p id="result"></p>
<script>
// Create a task Array
let tasks = [];
</script>
</body>
</html>

Result View Example
Add styling to distinguish input boxes, error layout styles, and valid confirmation elements:
<style>
input {
padding: 8px;
width: 260px;
margin-bottom: 4px;
}
.error {
color: red;
margin: 0;
}
.ok {
color: green;
margin: 0;
}
.field {
margin-bottom: 12px;
}
</style>

Result View Example
Cache inputs and build common error layout handlers:
const form = document.getElementById("signupForm");
const nameInput = document.getElementById("name");
const emailInput = document.getElementById("email");
const passInput = document.getElementById("password");
const confirmInput = document.getElementById("confirm");
const nameError = document.getElementById("nameError");
const emailError = document.getElementById("emailError");
const passError = document.getElementById("passwordError");
const confirmError = document.getElementById("confirmError");
const result = document.getElementById("result");
function showError(el, message) {
el.innerHTML = message;
}
function clearError(el) {
el.innerHTML = "";
}
form.addEventListener("submit", function (event) {
event.preventDefault();
result.innerHTML = "";
if (validateForm()) {
result.innerHTML = "Form is valid!";
result.className = "ok";
} else {
result.innerHTML = "Please fix the errors.";
result.className = "error";
}
});

Result View Example
Implement individual validation checks:
function validateName() {
let value = nameInput.value.trim();
if (value.length < 2) {
showError(nameError, "Name must be at least 2 characters.");
return false;
}
clearError(nameError);
return true;
}
function validateEmail() {
let value = emailInput.value.trim();
if (!(/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value))) {
showError(emailError, "Enter a valid email address.");
return false;
}
clearError(emailError);
return true;
}
function validatePassword() {
let value = passInput.value;
if (value.length < 8) {
showError(passError, "Password must be at least 8 characters.");
return false;
}
clearError(passError);
return true;
}
function validateConfirm() {
let pass = passInput.value;
let confirm = confirmInput.value;
if (confirm === "") {
showError(confirmError, "Please confirm your password.");
return false;
}
if (confirm !== pass) {
showError(confirmError, "Passwords do not match.");
return false;
}
clearError(confirmError);
return true;
}
function validateForm() {
let okName = validateName();
let okEmail = validateEmail();
let okPass = validatePassword();
let okConfirm = validateConfirm();
return okName && okEmail && okPass && okConfirm;
}

Result View Example
Show an alert or custom styling if something is wrong.
Check email format using regex (already covered in Step 4).
Show a green success message next to fields that validate successfully.
Update display functions to show success styles when parameters match successfully:
function showError(el, message) {
el.innerHTML = message;
el.className = "error";
}
function showSuccess(el, message) {
el.innerHTML = message;
el.className = "ok";
}
function validateName() {
let value = nameInput.value.trim();
if (value.length < 2) {
showError(nameError, "Name must be at least 2 characters.");
return false;
}
showSuccess(nameError, "Name is valid.");
return true;
}

Result View Example
Elevate the interface with advanced real-world features:
input and blur events so users receive instant feedback.
Result View Example
Document in project
You can Download PDF file.