Question: 8 . 1 7 LAB: User registration form ( JavaScript ) lab activity 8 . 1 7 . 1 : LAB: User registration form (
LAB: User registration form JavaScript
lab activity
: LAB: User registration form JavaScript
Overview
In this lab, you will use regular expressions with JavaScript to validate a user registration form. Error messages will be displayed at the bottom of the form, as shown below.
Step : Inspect the project
The project contains HTML CSS and JavaScript files. The register.js file registers a click event handler for the Register button that prevents the form from submitting. The event handler calls checkForm to perform data validation.
Step : Implement checkForm
Complete checkForm in register.js to verify that the userprovided information is valid.
If form validation errors exist:
Display the formErrors
function checkForm
const formErrors document.getElementByIdformErrors;
const fullName document.getElementByIdfullname;
const email document.getElementByIdemail;
const password document.getElementByIdpassword;
const confirmPassword document.getElementByIdconfirmpassword';
let errors ;
Clear previous error states
formErrors.innerHTML ;
fullName email, password, confirmPasswordforEachinput
if input input.classList.removeerror;
;
Check if form is empty
const isFormEmpty fullName?.value && email?.value && password?.value && confirmPassword?.value;
if isFormEmpty
errors.pushMissing full name.";
errors.pushInvalid or missing email address.";
errors.pushPassword must be between and characters.";
errors.pushPassword must contain at least one lowercase character.";
errors.pushPassword must contain at least one uppercase character.";
errors.pushPassword must contain at least one digit.";
fullName email, password, confirmPasswordforEachinput
if input input.classList.adderror;
;
else
Individual field validation
if fullName?.value fullName.value.trimlength
errors Missing full name.";
fullName?.classList.adderror;
else if email?.value azAZ@azAZazAZ$testemailvalue
errors Invalid or missing email address.";
email?.classList.adderror;
else if password?.value password.value.length password.value.length
errors Password must be between and characters.";
password?.classList.adderror;
else if aztestpasswordvalue
errors Password must contain at least one lowercase character.";
password?.classList.adderror;
else if AZtestpasswordvalue
errors Password must contain at least one uppercase character.";
password?.classList.adderror;
else if testpasswordvalue
errors Password must contain at least one digit.";
password?.classList.adderror;
else if passwordvalue confirmPassword?.value
errors Password and confirmation password don't match.";
confirmPassword?.classList.adderror;
Display errors
if errorslength
const ul document.createElementul;
errors.forEacherror
const li document.createElementli;
litextContent error;
ulappendChildli;
;
formErrors.appendChildul;
formErrors.classList.removehide;
else
formErrors.classList.addhide;
document.getElementByIdsubmitaddEventListenerclick functionevent
checkForm;
event.preventDefault;
;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
