Not seeing your code gets a little complicated to answer for sure, but I believe it’s because the standard behavior of Submit is being fired.
You can use Event.preventDefault
to prevent action default. After doing the validation, if everything is correct you can use HTMLFormElement.submit()
to submit the form.
var input = document.querySelector('input'),
form = document.querySelector('form');
form.addEventListener('submit', validateAndSubmit, false);
function validateAndSubmit(event) {
// Prevenindo o comportamento padrão.
event.preventDefault();
// Pegando o valor do input.
var value = input.value;
// Fazendo a validação e enviando caso esteja OK.
if (value.length < 5)
alert('O formulário não será enviado, a palavra não possui 5 caracteres.');
else {
alert('O formulário será enviado.');
form.submit();
}
}
It is not possible to simulate sending the form in the Stackoverflow snippets, so I put the script in that fiddle.
Post the code of your form for us to analyze.
– user28595