28,921 views
5 votes
5 votes
Validate that the user age field is at least 21 and at most 35. If valid, set the background of the field to LightGreen and assign true to userAgeValid. Otherwise, set the background to Orange and userAgeValid to false.HTML JavaScript 1 var validColor-"LightGreen"; 2 var invalidColor"Orange" 3 var userAgeInput -document.getElementByIdC"userAge"); 4 var formWidget -document.getElementByIdC"userForm"); 5 var userAgeValid - false; 7 function userAgeCheck(event) 10 11 function formCheck(event) 12 if (luserAgeValid) { 13 14 15 16 17 userAgeInput.addEventListener("input", userAgeCheck); 18 formWidget.addEventListener('submit', formCheck); event.preventDefault); 3 Check Iry again

User Humberd
by
2.8k points

1 Answer

27 votes
27 votes

Answer:

Step-by-step explanation:

The code provided had many errors. I fixed the errors and changed the userAgeCheck function as requested. Checking the age of that the user has passed as an input and changing the variables as needed depending on the age. The background color that was changed was for the form field as the question was not very specific on which field needed to be changed. The piece of the code that was created can be seen in the attached image below.

var validColor = "LightGreen";

var invalidColor = "Orange";

var userAgeInput = document.getElementById("userAge");

var formWidget = document.getElementById("userForm");

var userAgeValid = false;

function userAgeCheck(event) {

if ((userAgeInput >= 25) && (userAgeInput <= 35)) {

document.getElementById("userForm").style.backgroundColor = validColor;

userAgeValid = true;

} else {

document.getElementById("userForm").style.backgroundColor = invalidColor;

userAgeValid = false;

}

};

function formCheck(event) {};

if (!userAgeValid) {

userAgeInput.addEventListener("input", userAgeCheck);

formWidget.addEventListener('submit', formCheck);

event.preventDefault();

}

Validate that the user age field is at least 21 and at most 35. If valid, set the-example-1
User SkyRar
by
2.8k points