我有一个包含 8 个文本区域的表单验证 - 其中只有 1 个具有不同的条件。如果少于 140 个字符,则检查其中的 7 个。我对 JavaScript 相当陌生,想知道如何将其分解为 2 个函数,1 个用于少于 140 个字符,另一个用于字数统计。
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);
}
}
}
});