I've got a form validation that has 8 textareas - only 1 of them is with a different condition. 7 of them are checked if they have less than 140 characters. I'm fairly new to JavaScript and was wondering how can I break it down into 2 functions, 1 for the less than 140 characters and the other one is for word count.
inputTextAreas.forEach(input => {
let inputValue = input.value;
input.classList.remove("error");
if(input.name == 'question1') {
if (input.value.length < 10 || input.value.length > 140) {
$self.outputInputError(input,inputTextAreasErrors.question1);
}
} else if (input.name == 'question2') {
if (input.value.length < 10 || input.value.length > 140) {
$self.outputInputError(input,inputTextAreasErrors.question2);
}
} else if (input.name == 'question3') {
if (input.value.length < 10 || input.value.length > 140) {
$self.outputInputError(input,inputTextAreasErrors.question3);
}
} else if (input.name == 'question4') {
if (questionFourWordCount.length < 2 || questionFourWordCount.length > 7) {
$self.outputInputError(input,inputTextAreasErrors.question4);
}
} else if (input.name == 'question5') {
if (input.value.length < 10 || input.value.length > 140) {
$self.outputInputError(input,inputTextAreasErrors.question5);
}
} else if (input.name == 'question6') {
if (input.value.length < 10 || input.value.length > 140) {
$self.outputInputError(input,inputTextAreasErrors.question6);
}
} else if (input.name == 'question7') {
if (input.value.length < 10 || input.value.length > 140) {
$self.outputInputError(input,inputTextAreasErrors.question7);
}
} else {
if (input.name == 'question8') {
if (input.value.length < 10 || input.value.length > 140) {
$self.outputInputError(input,inputTextAreasErrors.question8);
}
}
}
});