Field validation via jQuery correctly

Asked

Viewed 82 times

1

I have the following validation for the TextBox justification. But even returning the message and clicking the OK button the system validates the deletion.

The right, should return the message and give a Focus in the justification field, thus preventing the system accepted without justification and/or less than 15 characters.

inserir a descrição da imagem aqui

$("[id$=btnExcluirPedido]").click(function () {
    var txtJust = $("[id$=txtJustificativaExc]");
    if (txtJust.val().length <= 15 || txtJust.val() == "") {
        $("[id$=txtJustificativaExc]").focus();
        alert("A justificativa deve conter no mínimo 15 caracteres.");
    }

    else {
        cancelPostback = false;
        GrabPageRequestManager();
    }

});
  • What is done after this check in the normal flow? An Ajax call? A common POST?

  • @LINQ only one post, in this case validates the exclusion in code Behind. But it should only enter this part if it respects the 15 minimum characters.

  • You’ll have to cancel the form Ubmit. There is no place trying to interrupt the normal flow of things, the only thing the code asks to do is show an Alert.

  • @LINQ all right, I’ll try this way.

1 answer

2


Try to use the method event.preventDefault() to prevent the action of post be held, so:

$("[id$=btnExcluirPedido]").click(function (event) {
    var txtJust = $("[id$=txtJustificativaExc]");
    if (txtJust.val().length <= 15 || txtJust.val() == "") {
        $("[id$=txtJustificativaExc]").focus();
        alert("A justificativa deve conter no mínimo 15 caracteres.");
        event.preventDefault();
    }

    else {
        cancelPostback = false;
        GrabPageRequestManager();
    }

});

Browser other questions tagged

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