0% found this document useful (0 votes)
5 views2 pages

Form Validation Js

The document describes a form validation function that validates user input fields including name, email, phone, password, and confirm password. It checks the length and format of each field and displays error messages for invalid values.

Uploaded by

eshwarr909
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

Form Validation Js

The document describes a form validation function that validates user input fields including name, email, phone, password, and confirm password. It checks the length and format of each field and displays error messages for invalid values.

Uploaded by

eshwarr909
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

function validateForm(event) {

event.preventDefault(); // Prevent form submission for now

// Fetching input values


const name = document.getElementById('name').value.trim();
const email = document.getElementById('email').value.trim();
const phone = document.getElementById('phone').value.trim();
const password = document.getElementById('password').value;
const confirmPassword = document.getElementById('confirmPassword').value;

const nameError = document.getElementById('nameError');


const emailError = document.getElementById('emailError');
const phoneError = document.getElementById('phoneError');
const passwordError= document.getElementById('passwordError');
const confirmPasswordError =
document.getElementById('confirmPasswordError');

// Validation rules
if (name.length < 5) {
nameError.innerHTML='username should be more than 5 characters'

}
else{
nameError.innerHTML=''

if (!email.includes('@')) {
emailError.innerHTML="enter valid email";

}
else{
emailError.innerHTML="";

if (phone === '123456789' || !/^\d{10}$/.test(phone)) {

phoneError.innerHTML="enter valid mobile number"


}
else{
phoneError.innerHTML=""
}

if (password.length < 8 || password.toLowerCase() === 'password' ||


password.toLowerCase() === name.toLowerCase()) {
passwordError.innerHTML="enter valid password"
}
else{
passwordError.innerHTML=""

if (password !== confirmPassword) {

confirmPasswordError.innerHTML="Both passwords should match"


}
else{
confirmPasswordError.innerHTML=""
}

You might also like