Field validation via jQuery correctly

1

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

That's right, you should return the message and focus on the justification field, thus preventing the system from accepting without justification and / or less than 15 characters.

$("[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();
    }

});
    
asked by anonymous 18.08.2017 / 16:02

1 answer

2

Try to use the event.preventDefault() method to prevent the post action from being performed, like this:

$("[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();
    }

});
    
18.08.2017 / 19:57