Im a rookie in coding, an im figuring a way to shorten this code, it has too many if statements, if someone can help me out, i really apreciatted. I need to add a classList.remove to the same elements too after that
So this is the code:
const inputElementName = document.getElementById("name");
const inputElementMail = document.getElementById("email");
const validateInputName = () => inputElementName.value.trim().length > 0;
const validateInputMail = () => inputElementMail.value.trim().length > 0;
const handleInputName = () => {
const inputNameIsValid = validateInputName();
if (!inputNameIsValid) {
return inputElementName.classList.add("error");
}
}
const handleInputMail = () => {
const inputMailIsValid = validateInputMail();
if (!inputMailIsValid) {
return inputElementMail.classList.add("error");
}
}
Try this
const validateInput = (elm) => elm.value.trim().length > 0;
const handleInput = (elm) => {
const isValid = validateInput(elm);
elm.classList.toggle("error", !isValid);
return isValid;
}