Question

Consider an HTML form:

<form action="" method="POST" onsubmit="return checkValidArray()">
    <input type="text" name="data" onchange="return validate(this);">
</form>

It appears (in both IE 6 and Firefox 3) that when you type some text into the input field and click submit that the onchange event fires for the input field, but the onsubmit event does not fire for the form until you click the submit button a second time (at which time the onchange does not fire, of course). In other words, the onchange seems to prevent the onsubmit from firing.

The desire is to check each field in the form when the user exits the field in order to provide them with immediate validation feedback, and also to prevent submission of the form if there is invalid data.

[EDIT: Note that both validation functions contain an alert().]

How does one get around this problem?

Was it helpful?

Solution

Solution (of a sort):

It turns out that it is only presence of an alert() - or a confirm() - during the input's onchange event that causes the form's onsubmit event to not fire. The JS thread appears to get blocked by the alert().
The workaround is to not include any alert() or confirm() in the input's onchange call.

Browsers known to be affected:
IE6 - Windows
IE8 - Win
FF3 - Win

Browsers known not to be affected:
Google Chrome - Win
Safari - Mac
FF - Mac

OTHER TIPS

I tried with Firefox 3 (Mac) with the following code:

    <html>
        <head>
            <script>
                function validate(ele)
                {
                    alert("vali");
                    return false;
                }

                function checkValidArray()
                {
                    alert("checkValidArray");
                }

            </script>
        </head>
        <body>
            <form action="" method="POST" onsubmit="return checkValidArray()">
                <input type="text" name="data" onchange="return validate(this);">
                <input type="submit" value="Ok">
            </form>
        </body>
</html>

It seems to work. When I click on the Ok button, both "Vali" and "Check Valid Array" pop up. Initially I thought return false could be the reason why the form was not submitted, but it IS submitted (at least, checkValidArray() is called).

Now, what are you doing in your checkValidArray() and validate() methods? Something special? Can you post the code?

EDIT: I tested with IE8 and FF3 on windows, and here both events do not get fired. I have no idea why. Perhaps onblur IS a solution, but WHY does onchange not work? Is there a specific reason or is it just another inconsistency? (Works on both FF and Safari on Mac)

You could use the onblur event in your inputs instead of onchange.

It's quite interesting that the behaviour is different on the Mac, so it appears platform dependent and not browser dependent. Must be a clue there somewhere...

I tried onblur with Nivas' code but had the same result as with onchange (only the "Vali" alert).

What does make a difference is whether you do anything in validate(). Comment out the line alert("vali"); and it works! (The actual return value from validate() does not matter, although I wouldn't expect it to.)

EDIT: A colleague just tried this in Google Chrome on Windows and it works there. This intuitively makes sense because of how Chrome separates JS threads.

Something about the first alert() blocking the thread causes the onsubmit event to get lost. Possible race condition?

Don't create ; submit button. Instead, create a normal button and write a function which can be called onClick with that button.

Function will do the validation on the form fields, and if everything is fine, it will submit the form. Otherwise it will not.

function validate
{
  "Piece of code to Validate your form"
  document.formName.action.value = "URL which you want to call"
 document.propFile.submit(); // It will submit he page
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top