Loading lesson path
Concept visual
Start at both ends
In this project you will build a form with validation. The form will show error messages under each input field, and it will not submit until everything is valid.
Formula
Step 1 - Create the HTMLCreate a form with four fields and an error message under each field.
<form id="signupForm"> <div class="field">
Formula
< label > Name:</label >< br ><input id="name" type="text" placeholder="Your name"> <p id="nameError" class="error"></p> </div> <form>
Formula
Step 2 - Add the CSS<style>
input {
padding: 8px;
width: 260px;
margin-bottom: 4px;
}.error {
color: red;
margin: 0;
}.ok {
color: green;
margin: 0;
}.field {
margin-bottom: 12px;
}</style>
Formula
Step 3 - Add JavaScriptYou must create an object for each field.
// Create an Object for each Field 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");You need a function to diplay errors.
// Function to Display Error function showError(el, message) {
el.innerHTML = message;
}You need a function to clear the error field.
// Function to Clear the Error function clearError(el) {
el.innerHTML = "";
}You need a function to validate the form.
// Function to Validate Form function validateForm() {
return false;
}You need the code to validate the form. To validate the form, you must stop the page from reloading.
// Prevent Default Reloading form.addEventListener("submit", function (event) {
event.preventDefault();
// Clear Result result.innerHTML = "";
// Validate Form if (validateForm()) {
result.innerHTML = "Form is valid!";
result.className = "ok";
} else {
result.innerHTML = "Please fix the errors.";
result.className = "error";
}
});Formula
Step 4 - Validate FieldsCreate one function per field.
// Function to Validate Name 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 to Validate Form function validateForm() {
let okName = validateName();
return okName;
}// Function to Validate Email 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 to Validate Form function validateForm() {
let okName = validateName();
let okEmail = validateEmail();
return okName && okEmail;
}// Function to Validate Password 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 to Validate Form function validateForm() {
let okName = validateName();
let okEmail = validateEmail();
let okPass = validatePassword();
return okName && okEmail && okPass;
}// Function to Validate Confirm 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 to Validate Form function validateForm() {
let okName = validateName();
let okEmail = validateEmail();
let okPass = validatePassword();
let okConfirm = validateConfirm();
return okName && okEmail && okPass && okConfirm;
}
Form Validation (Finished)This example shows the finished project.
<style>
input {
padding: 8px;
width: 260px;
margin-bottom: 4px;
}.error {
color: red;
margin: 0;
}.ok {
color: green;
margin: 0;
}.field {
margin-bottom: 12px;
}</style>
<!DOCTYPE html> <html> <style>... CSS goes here </style> <body>
Formula
< h2 > Sign Up </h2 ><form id="signupForm"> <div class="field">
Formula
< label > Name:</label >< br ><input id="name" type="text" placeholder="Your name"> <p id="nameError" class="error"></p> </div> <div class="field">
Formula
< label > Email:</label >< br ><input id="email" type="text" placeholder="name@example.com"> <p id="emailError" class="error"></p> </div> <div class="field">
Formula
< label > Password:</label >< br ><input id="password" type="password" placeholder="Min 8 characters"> <p id="passwordError" class="error"></p> </div> <div class="field">
Formula
< 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> // JavaScript goes here </script> </body> </html>
// Create an Object for each Field 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 to Display Error function showError(el, message) {
el.innerHTML = message;
}
// Function to Clear Error function clearError(el) {
el.innerHTML = "";
}
// Function to Validate Name 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 to Validate Email 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 to Validate Password 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 to Validate Confirm 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 to Validate Form function validateForm() {
let okName = validateName();
let okEmail = validateEmail();
let okPass = validatePassword();
let okConfirm = validateConfirm();
return okName && okEmail && okPass && okConfirm;
}
// Prevent Default Reloading form.addEventListener("submit", function (event) {
event.preventDefault();
// Clear Result result.innerHTML = "";
// Validate Form if (validateForm()) {
result.innerHTML = "Form is valid!";
result.className = "ok";
} else {
result.innerHTML = "Please fix the errors.";
result.className = "error";
}
});Forgetting event.preventDefault()
with user input (use text only for messages)
Add a rule that the password must contain at least one number.