I am currently developing a complex multi-form using bootstrap. Within my validateForm function, I have incorporated logic to add an "Invalid" class if the input value is empty, indicating that the user has not entered any information.
When the user clicks on the next button, the input field will display a reddish background color, signifying that it requires input.
My question is, how can I remove the "Invalid" class in two scenarios:
- When the user starts typing in the input field
OR
- When the user clicks or focuses on the input field
This action should reset the background color back to its original state.
Here is the JavaScript code snippet:
function validateForm() {
var x, y, i, valid = true;
x = document.getElementsByClassName("tab");
y = x[currentTab].getElementsByTagName("input");
for (i = 0; i < y.length; i++) {
if (y[i].value == "") {
y[i].className += " invalid";
valid = false;
}
}
if (valid) { document.getElementsByClassName("step")[currentTab].className += " finish"; }
return valid;
}
And here is the relevant CSS code:
input.invalid {
background-color: #ffdddd;
}
Your help and guidance are highly appreciated!