0
I’m trying to run a function in the form Ubmit, it works correctly, but if return true
I need the form not sent.
It’s being done this way:
$('#FornecedorNovo').submit(function (e) {
var url = "/Fornecedor/VerificaInscricao";
var Insc = $("#InscricaoEstadual").val();
$.ajax({
url: url,
data: { insc: Insc },
datatype: "json",
type: "POST",
success: function (data) {
if (data.resultado == true) {
alert('Já existe esta inscrição estadual cadastrada para outro fornecedor.');
e.preventDefault();
}
}
})
});
But still the form is sent.
Puts a
return false;
– Pedro Augusto
You could make the ajax request synchronous, but ideally let the form be sent and do this validation on the server.
– fernandosavio
The event is asynchronous, so the function will end before you get the answer via AJAX. Thus, the
e.preventDefault()
will never run, leaving the form to be submitted normally. But the question is: and if it returns false, what should happen?– Woss
If it returns false the form must be sent, it is sending anyway. In another function I do in Submit I use the
e.preventDefault()
and it works.– Mariana
@Pedroaugusto I tried, but still the form is sent.
– Mariana
What is the best way to check ?
– Mariana