John
John

Reputation: 5834

How to stop onkeyup event being fired when an alert box is cleared with the enter key

I have a text field which uses the JavaScript onkeyup event to do some "quiet" validation with a tick or cross if the value is valid.

When the enter key is pressed, it does the same validation, but with an alert box advising of any problems. If the enter key is used to clear the alert box, this press of the enter key refires the onkeyupevent, which restarts the validation and the alert boxes again.

As soon as I click the OK in the alert box, this naturally stops.

Is there any way I can detect that the enter press was from the alert box?

This definitely happens in Opera, Safari and Firefox.

Upvotes: 0

Views: 2831

Answers (2)

Chris Farmiloe
Chris Farmiloe

Reputation: 14185

Use the keypress event instead when triggering the behavior of the enter-key.

Upvotes: 2

meouw
meouw

Reputation: 42140

Can you ignore the enter key in your onkeyup event handler?

textbox.onkeyup = function( e ) {
    if( e.keyCode == 13 ) {
        return;
    }
    /* your validation here */
}

Upvotes: 1

Related Questions