Project - Form Validation
In this project you will build a form with validation.
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.
What You Will Learn
- How to validate form fields with JavaScript
- How to prevent a form from submitting
- How to show error messages under inputs
- How to check email and password rules
Share
Step 1 - Create the HTML
Create a form with four fields and an error message under each field.
Example
Example Code
htmlName:Step 2 - Add the CSS
Example
Example Code
htmlinput {
padding: 8px;
width: 260px;
margin-bottom: 4px;
}
.error {
color: red;
margin: 0;
}
.ok {
color: green;
margin: 0;
}
.field {
margin-bottom: 12px;
}Step 3 - Add JavaScript
You must create an object for each field.
Example
Example Code
javascript// 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.
Example
Example Code
javascript// Function to Display Error
function showError(el, message) {
el.innerHTML = message;
}You need a function to clear the error field.
Example
Example Code
javascript// Function to Clear the Error
function clearError(el) {
el.innerHTML = "";
}You need a function to validate the form.
Example
Example Code
javascript// 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.
Example
Example Code
javascript// 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";
}
});Step 4 - Validate Fields
Create one function per field.
Validate Name
Example Code
javascript// Function to Validate Name
function validateName() {
let value = nameInput.value.trim();
if (value.length
input {
padding: 8px;
width: 260px;
margin-bottom: 4px;
}
.error {
color: red;
margin: 0;
}
.ok {
color: green;
margin: 0;
}
.field {
margin-bottom: 12px;
}The HTML
Example Code
html... CSS goes here
## Sign Up
Name:
Email:
Password:
Confirm Password:
Create Account
// JavaScript goes hereThe JavaScript
```javascript // 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 JavaScript Projects > JavaScript Counter > A counter project with restore and save in localStorage. > JavaScript Event Listener > The counter project using event listener instead of onclick. > JavaScript To-Do List > A do-do list saved in an array in local storage. > JavaScript Modal Popup > A modal popup window that appears on top of the page. > JavaScript Form Validation > An HTML Form with form validation.
Interactive Code Examples
JS Example 1
htmlJS Example 2
html<style>
input {
padding: 8px;
width: 260px;
margin-bottom: 4px;
}
.error {
color: red;
margin: 0;
}
.ok {
color: green;
margin: 0;
}
.field {
margin-bottom: 12px;
}
</style>JS Example 3
javascript// 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");JS Example 4
javascript// Function to Display Error
function showError(el, message) {
el.innerHTML = message;
}JS Example 5
javascript// Function to Clear the Error
function clearError(el) {
el.innerHTML = "";
}JS Example 6
javascript// Function to Validate Form
function validateForm() {
return false;
}