0

Actually, when my form is submitted with the submit button, I would want that the javascript code is executed for checking datas and if datas are not filled, to open a popup dialog and so, don't execute the action bounded to the form.

Unfortunately, the javascript code is never executed even if the javascript is on the top of the page.

Do you have a solution ?

Thank you

My form :

@using (Html.BeginForm("AddRecipe", "AddRecipe", FormMethod.Post, new { data_ajax = "false"}))
{
//Set of components to submit
//etc
<input type="submit" data-theme="b" id="addRecipeBtn" onclick="checkDatas();" value="Ajouter ma recette" />
}

My javascript code to execute :

<script>
    function checkDatas() {
            alert("ici");
            var ok = $("#fakeInput").val().length != 0 &&
                 $("#title").val().length != 0 &&
                 $("#cookingTime").val().length != 0 &&
                 $("#preparationTime").val().length != 0 &&
                 $("#ingredientListArea").val().length != 0 &&
                 $("#preparationArea").val().length != 0;

            if (!ok) {
                $("#popupDialog").popup("open");
            }
    }
</script>
2
  • Are there any errors on the JavaScript console? If you put a breakpoint in the code, is it truly not executed at all or does it execute and then fail in some way? Commented Aug 10, 2014 at 21:50
  • Appears to be working fine in a simpler form on plunker. Commented Aug 10, 2014 at 21:51

3 Answers 3

1

We will need more information to determine if or why it's not executing at all.

But in order to prevent the form from submitting when validation fails, use event.preventDefault(). This, of course, will prevent the default behavior of the button from executing...in your case, it will prevent form submission.

function checkDatas(event) {
        alert("ici");
        var ok = $("#fakeInput").val().length != 0 &&
             $("#title").val().length != 0 &&
             $("#cookingTime").val().length != 0 &&
             $("#preparationTime").val().length != 0 &&
             $("#ingredientListArea").val().length != 0 &&
             $("#preparationArea").val().length != 0;

        if (!ok) {
            event.preventDefault();
            $("#popupDialog").popup("open");
        }
}
Sign up to request clarification or add additional context in comments.

Comments

1

I think you will find your form submit is overriding your onclick handler, it would be more reliable in my opinion to catch the form onsubmit event, since the button is of type "submit"

<form onsubmit="checkDatas()">

removing the onclick event of your button.

Comments

0

Are you preventing the default submit action of a html form submit?

I would think you need something like this:

function checkDatas(e) {
  e.preventDefault();
  //the rest of your code

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.