0% found this document useful (0 votes)
2 views

Script

The document contains JavaScript code for a registration form validation. It prevents form submission until all fields are validated, checking for required name, valid email format, strong password criteria, and matching password confirmation. If all validations pass, a success alert is displayed, and the form can be submitted or further actions can be taken.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Script

The document contains JavaScript code for a registration form validation. It prevents form submission until all fields are validated, checking for required name, valid email format, strong password criteria, and matching password confirmation. If all validations pass, a success alert is displayed, and the form can be submitted or further actions can be taken.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

document.getElementById('registrationForm').

addEventListener('submit', function (e)


{
e.preventDefault(); // Prevent form submission to handle validation

// Clear previous error messages


clearErrors();

// Get form fields


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

let isValid = true;

// Validate Name
if (name.trim() === "") {
isValid = false;
showError('nameError', "Name is required");
}

// Validate Email
const emailPattern = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
if (!emailPattern.test(email)) {
isValid = false;
showError('emailError', "Please enter a valid email");
}

// Validate Password
const passwordStrengthPattern = /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d@$!%*?&]{8,}$/;
if (!passwordStrengthPattern.test(password)) {
isValid = false;
showError('passwordError', "Password must be at least 8 characters long and
contain at least one letter and one number");
}

// Validate Password Confirmation


if (password !== confirmPassword) {
isValid = false;
showError('confirmPasswordError', "Passwords do not match");
}

// If all validations pass, submit the form (or you can do further actions here)
if (isValid) {
alert("Form submitted successfully!");
// Optionally: you can submit the form here using form.submit() or perform
other actions.
}
});

// Function to show error messages dynamically


function showError(elementId, message) {
const errorElement = document.getElementById(elementId);
errorElement.textContent = message;
errorElement.style.display = 'block';
}

// Function to clear error messages


function clearErrors() {
const errorMessages = document.querySelectorAll('.error-message');
errorMessages.forEach(error => {
error.textContent = '';
error.style.display = 'none';
});
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy