Without seeing your code it gets a bit tricky to answer for sure, but I believe it's because the default behavior of submit is being triggered.
You can use Event.preventDefault
to prevent default action. 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();
}
}
You can not simulate sending the form in the StackOverflow snippets, so I put the script in this fiddle .