Confirm before submitting javascript function

Asked

Viewed 874 times

4

When I submit a form, by onsubmit(checkFormIsCorrect()), call this javascript function that will validate various situations and submit or not the form.

In this capacity:

function checkFormIsCorrect() {
    if (!$('div.has-error').length > 0) {
        return displayAlertify('confirm',
                'Confirme todos os dados antes de submeter');
    }
    return false;
}

The function displayAlertify(...) shows a box with the buttons "OK" and "CANCEL", returning true or false.

The problem is that I can’t stop code execution until I receive the function’s Return displayAlertify(...).

  • did not miss the e.preventDefault()?

  • posts your html code.

  • I couldn’t understand your problem properly. What is your goal?

  • <form onsubmit="Return checkFormIsCorrect();" class="form_1" method="post"> .... ..... <div class="form_line"> <button type="Submit" class="btn btn2 btn-Primary btn-block" name="submit_new_user">Register</button> </div> </form>

  • The goal is to be able to confirm or cancel the submission of the form by clicking on the 'OK' or 'CANCEL' buttons'

  • You should show how you declare displayAlertify

  • Function displayAlertify(message) { alertify.confirm(message, Function(e) { if(e) { Return true; } Else { Return false; } }); }

Show 2 more comments

2 answers

3


The function displayAlertify returns nothing.

*expanded comment code*

function displayAlertify(message) {

    /* escopo anterior */

    alertify.confirm(message, function(e) {

        /* novo escopo */

        if (e) {
            /* retorna para onde foi chamado */
            return true;
        } else
            /* retorna para onde foi chamado */
            return false;
    });
}

- The callback in alertify.confirm does not perform the return in the previous scope. Indeed, it would not make sense.

Moreover, since the callback of onsubmit executes, it is not possible to return a value to you yet, because the execution expires. This is so with all functions. So, when the user probably confirms the alert, the callback of onsubmit has already expired before.

And that’s enough, break the sending of the form until the alert alertify is confirmed, then send again.

function checkFormIsCorrect() {
    if ($('div.has-error').length <= 0) {
        displayAlertify('confirm', 'Confirme todos os dados antes de submeter');
    }
    /* break */
    return false;
}

function displayAlertify(message) {
    alertify.confirm(message, function(e) {
        /* submete o formulário */
        if (e) $('form')[0].submit();
    });
}

1

Problem solved.

Stayed like this:

function checkFormIsCorrect() {
    if (!$('div.has-error').length > 0) {
         displayAlertify('confirm',
                 'Confirme todos os dados antes de submeter');
    }
    return false;
}

function displayAlertify(type, message) {
    alertify.confirm(message, function(e) {
        if (e) $('form')[0].submit();
        e.preventDefault();
    });
 }
  • 1

    I put the false Return out of the go because if the if block is not executed, the function must return false.

  • I had forgotten that you declared the !, now I understand... x_X why you returned false. I changed the operator > for <= and took the ! in my answer, so maybe it’s less confusing

  • 1

    I am able to submit and cancel the submission when I want. It submits the form when it enters both if of each function. Summarizing: execution starts by entering the function checkFormIsCorrect(), enters if(...), enters the function displayAlertify(...) and makes the form ubmit: If it does not enter the first if, returns false.

Browser other questions tagged

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