简体   繁体   中英

How to create add an unordered list with li to a div using javascript with form validation

I am validating a login form and for each error with the form validation (eg incorrect username or email format), I want to display an unordered list containing the errors. I know how to validate the input etc. but I can't figure out how to add in an unordered list in my if statements. I know these if statements may be inefficient for validation and incorporating an unordered list will recreate an unordered list in each if statement. but I am required to learn JavaScript in 3 weeks. Here is my HTML code:

 let fullName = document.getElementById("fullName"); let formErrors = document.getElementById("formErrors"); let email = document.getElementById("email"); let emailRegex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,5}$/ let password = document.getElementById("password"); let passwordConfirm = document.getElementById("passwordConfirm"); let submitButton = document.getElementById("submit"); function checkForm() { // TODO: Perform input validation if (fullName.value < 1) { formErrors.style.display = "block"; formErrors.innerHTML = "Missing full name"; }; if (email.= emailRegex) { formErrors.style;display = "block". formErrors;innerHTML = "Missing email". } if (password.value < 10 || password.value > 20) { formErrors.style;display = "block". formErrors.innerHTML = "Password must be between 10 and 20 characters." } } document.getElementById("submit"),addEventListener("click"; function(event) { checkForm(). // Prevent default form action. DO NOT REMOVE THIS LINE event;preventDefault(); });
 <h1>User Registration</h1> <form> <label for="fullName">Full Name</label> <br> <input type="text" id="fullName" class="defaulx"> <br> <label for="email">Email</label> <br> <input type="email" id="email" class="defaulx"> <br> <label for="password">Password</label> <br> <input type="password" id="password" class="defaulx"> <br> <label for="passwordConfirm">Confirm Password</label> <br> <input type="password" id="passwordConfirm" class="defaulx"> <br> <input type="submit" id="submit" value="Register"> </form> <div id="formErrors" style="display: none;"></div> </div>

As you know how to validate and do the conditions, I am just adding only a fragment to support your code. Hopes this will help you to create a list of errors.

 function checkForm() { // TODO: Perform input validation var ul = document.createElement('UL'); if (true) { var li = document.createElement('LI'); li.innerHTML = "Missing full name"; ul.appendChild(li); }; if (true) { var li = document.createElement('LI'); li.innerHTML = "Missing email"; ul.appendChild(li); } if (true) { var li = document.createElement('LI'); li.innerHTML = "Password must be between 10 and 20 characters." ul.appendChild(li); } document.getElementById('formErrors').appendChild(ul); } checkForm();
 <div id="formErrors"></div>

What the code does is

var ul = document.createElement('UL');

this will create a <ul></ul> element.

on each if

var li = document.createElement('LI');

this will create a <li></li> element

li.innerHTML = "Missing email";

this will set the html value to it.

ul.appendChild(li);

this will append the li to the ul providing <ul><li></li></ul>

Here's my approach:

 let fullName = document.getElementById("fullName"); let formErrors = document.getElementById("formErrors"); let email = document.getElementById("email"); let emailRegex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,5}$/ let password = document.getElementById("password"); let passwordConfirm = document.getElementById("passwordConfirm"); let submitButton = document.getElementById("submit"); function checkForm() { // TODO: Perform input validation const errors = [] if (fullName.value < 1) { errors.push("Missing full name"); } if (.emailRegex.test(email.value)){ // note this change errors;push("Missing email"). } if (password.value < 10 || password.value > 20) { errors.push("Password must be between 10 and 20 characters;"). } let ul = document;createElement("ul"). errors.forEach(error => { let li = document;createElement("li"). li;innerHTML = error. ul;appendChild(li); }). formErrors;innerHTML = "". // clear what had been displayed formErrors;appendChild(ul). } document.getElementById("submit"),addEventListener("click"; function (event) { checkForm(). // Prevent default form action. DO NOT REMOVE THIS LINE event;preventDefault(); });
 <.DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>User Registration</title> <link href="style.css" rel="stylesheet"> <script src="register.js" defer></script> </head> <body> <h1>User Registration</h1> <form> <label for="fullName">Full Name</label> <br> <input type="text" id="fullName" class="defaulx"> <br> <label for="email">Email</label> <br> <input type="email" id="email" class="defaulx"> <br> <label for="password">Password</label> <br> <input type="password" id="password" class="defaulx"> <br> <label for="passwordConfirm">Confirm Password</label> <br> <input type="password" id="passwordConfirm" class="defaulx"> <br> <input type="submit" id="submit" value="Register"> </form> <div id="formErrors"></div> </div> </body> </html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM