Form submit with jquery

Asked

Viewed 72 times

0

I have this function to check the state registration and CNPJ/CPF fields and if they are right it performs the submit, only that in the submit, he is not respecting the required of the fields ViewModel

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

    $.ajax({
        url: url,
        data: { insc: Insc, isento: Isento },
        datatype: "json",
        type: "POST",
        success: function (data) {
            if (data.resultado == true) {
                $("#messageI").html(data.mensagem);
                if (data.mensagem != 'O campo Inscrição é obrigatório.') {
                    $("#InscricaoEstadual").val('');
                    $("#InscricaoEstadual").focus();
                }
            } else {
                var url1 = "/Fornecedor/VerificaDocumento";
                var Documento = $("#Documento").val();

                $.ajax({
                    url: url1,
                    data: { documento: Documento, tipo: Tipo },
                    datatype: "json",
                    type: "POST",
                    success: function (data) {
                        if (data.resultado == true) {
                            $("#message").html(data.mensagem);
                            $("#Documento").val('');
                            $("#Documento").focus();
                        }
                        else {
                            $form.off('submit').submit();
                        }
                    }
                });
            }
        }
    })
});

Wouldn’t it be right for him to do Submit only if the fields were correct? Because if the field State Registration and Document are in agreement, it sends anyway the data.

  • The required does not validate the fields, it just forces the user to type anything.

  • So I need them to be filled, and this way, ignore.

  • you are calling the Submit on the button? using required input did not work?

  • I set it this way: [Required(ErrorMessage = "O campo {0} é obrigatorio.")]
 [StringLength(150)]
 public string Nome { get; set; } in the dataannotations

  • Ta using some framework: Laravel, Ionic, Angular?

  • No, I work with ASP CORE, and I used the ViewModel to inform the required, @adventistaam when the field is not filled in, for example the name, which is required, but the two of this above rule are correct, it sends, ignoring the mandatory completion of the field.

  • you are checking the Asp side request with Modelstate.Isvalid?

  • @Lucasmiranda.

  • in my view what is happening is that required checks only for null, when you take with jquery the val it comes empty, but does not come null ("")

  • It appears the messages that the field is required and sends normally, so I believe that the problem is not this, only that as has this part in the code $form.off('submit').submit(); he is ignoring the required fields.

  • I won’t be able to help you much because it’s not very clear what you want to do with your request, but I can tell you that this tangle of requests and calling the form at the end is confusing, but still I think the problem is there on the side of the Asp, tries to add in your required this here: [Required(Allowemptystrings=false)] [Displayformat(Convertemptystringtonull=false)]

  • I do the validation on dataannotations, but when the data of this function above are correct and he falls into $form.off('submit').submit(); it does not validate the fields, but if I take this part, or put only return true; it validates the fields, but does not do Submit.

  • I saw that you do the validation with date Annotation, but you did not put anything to validate empty

  • @Lucasmiranda took the test and the same thing happens, in this part of the function in submit I can’t verify?

Show 9 more comments

1 answer

0


I was able to solve by adding this line to the submit:

   if ($('#FornecedorNovo').valid()) {
         $form.off('submit').submit();
   }

Browser other questions tagged

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