KingRichard
KingRichard

Reputation: 1244

onKeyUp triggering too early

I have the following if statement in a jquery function triggered by onKeyUp in a text input field

if ( famcount < 3 ) {
    alert('number too small.');
} else if ( famcount > 10 ){
    alert('number too large.');
}

The problem is, if the user tries to type 10 it executes after the 1 and triggers the first alert. Any suggestions on a better way to trigger this event than onKeyUp?

Upvotes: 1

Views: 95

Answers (2)

Mohd Abdul Mujib
Mohd Abdul Mujib

Reputation: 13948

you could use the .blur() event.

Because even if the user is trying to enter 5 digits the keyup event is supposed to fire 5 times so that's normal.

Or you could bind the validation to on user submit.

Upvotes: 2

George
George

Reputation: 36794

Don't make this check on every keystroke.

Use the blur event instead, and then check the value of the input.

A JSFiddle to demonstrate

Upvotes: 2

Related Questions