Я создал функции для проверки моей формы. Но я хочу, чтобы все они запускались сразу, когда я нажимаю кнопку отправки. Итак, у меня есть функция formValidate, а затем у меня есть firstNameValidate, lastNameValidate и т. Д.
У меня вопрос: как мне создать функцию formValidate для запуска имеющихся у меня функций, но ТОЛЬКО отправить форму, если все они верны?
function firstNameValidate() {
// Making sure that the firstname input is not blank
if (firstName.value.length == 0) {
// If the firstname input is blank, then return the error text below
error.innerHTML = 'Please Enter a Valid First Name, Cannot be Blank';
// Error text css class
error.className = 'error';
// Making sure that the browser window focuses on the error
firstName.focus();
// Does not let the browser submit the form
// this statement makes sure that the input has only letters
return false;
} else if (!firstName.value.match(letter)) {
// // If the input has something other then numbers, show this error.
error.innerHTML =
'Please Enter a Valid First Name, Cannot contain characters(!@#) or numbers';
// // error text css class
error.className = 'error';
// browser window focuses on error
firstName.focus();
// Form does not submit
return false;
}
if (firstName.value.length > 0 && firstName.value.match(letter)) {
error.className = '';
error.innerHTML = '';
return true;
}
}
Я могу получить имя и фамилию для проверки, однако, если один из них заполнен, он отправляет форму. Поэтому возвращение true и возвращение false я считаю неправильным.