Function jQuery return validation message instead of Alert

Asked

Viewed 289 times

0

I have this function, which works perfectly:

  $('#FornecedorNovo').submit(function (e) {
    e.preventDefault();
    var url = "/Fornecedor/VerificaInscricao";
    var Insc = $("#InscricaoEstadual").val();
    var form = this,
        $form = $(form); // Salvamos o formulário atual em uma variável

    $.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.');
                $("#InscricaoEstadual").focus();
            } else {
                var url1 = "/Fornecedor/VerificaDocumento";
                var Documento = $("#Documento").val();

                $.ajax({
                    url: url1,
                    data: { documento: Documento },
                    datatype: "json",
                    type: "POST",
                    success: function (data) {
                        if (data.resultado == true) {
                            alert('Já existe este CNPJ/CPF cadastrado para outro fornecedor.');
                            $("#Documento").focus();
                        }
                        else {
                            $form.off('submit').submit();
                        }
                    }
                });
            }
        }
    })
});

However it appears an Alert, I wanted the message to appear equal to field validation, no <span asp-validation-for="InscricaoEstadual" class="text-danger"></span> for example. It is possible?

  • You should probably add the framework tag you’re using. There may be a simpler way to do this that isn’t just javascript or jquery.

  • @fernandosavio added. work with Asp.net core.

1 answer

1


Since I don’t understand Asp.Net, a solution with jQuery would be to take the element you want to insert the error message with jQuery and change its HTML. In your case it would change the line:

alert('Já existe este CNPJ/CPF cadastrado para outro fornecedor.');

for:

$('[asp-validation-for="InscricaoEstadual"]').html('Já existe este CNPJ/CPF cadastrado para outro fornecedor.');

To improve performance you could save the result of $('[asp-validation-for="InscricaoEstadual"]') in a variable to not have to search for each ajax request, or better, define a id for the element and use a id selector instead of using the attribute selector used in the example.

  • I tried this way, but the message does not appear, and does not return error on the console.

  • He wasn’t accepting the '[asp-validation-for="InscricaoEstadual"] then I defined a id for him, and I passed on function $("#messageI").html(data.mensagem);and it worked, thank you.

  • I will add the attribute selector documentation to make the question better documented. But the way you solved, using id, is the most performative even.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.