0

I am trying to fire an event 3 seconds after the user has stopped typing to check the input to see if it meets the minimum length requirement. If it does, then show as valid and if less than 14 characters show as invalid/error. I want onkeydown to clear timer, check for positive validation, and if it fails to start another timer.

var typingTimer;                //timer identifier
var doneTypingInterval = 3000;  //time in ms, 3 second for example

// on keydown, start the countdown
$('#contact_phone').keydown(function(){
clearTimeout(typingTimer);
if ($('#contact_phone').val().length == 14) {
    typingTimer = setTimeout(doneTyping, doneTypingInterval);
}
});

//user is "finished typing," do something
function doneTyping () {
$('#contact_phone').keyup(function() {
    if ($(this).val().length == 14) {
        $(this).removeClass('whiteBorder').removeClass('error').addClass('valid');
    } else {
        $(this).removeClass('whiteBorder').removeClass('valid').addClass('error');
    }
});
}
1
  • Is it working for you? what problems are you having (if any?) Commented Dec 2, 2013 at 21:53

2 Answers 2

1

You are binding the keyup event every single time the timer expires.

You need not bind both the events to the input. ' Remove the keydown and replace it with keyup event.

var typingTimer; //timer identifier
var doneTypingInterval = 3000; //time in ms, 5 second for example

// on keydown, start the countdown
$('#contact_phone').keyup(function () {
    clearTimeout(typingTimer);
    typingTimer = setTimeout(doneTyping, doneTypingInterval);
});

//user is "finished typing," do something
function doneTyping() {
    var $this = $('#contact_phone');

    if ($this.val().length == 14) {
        $this.removeClass('whiteBorder').removeClass('error').addClass('valid');
    } else {
        $this.removeClass('whiteBorder').removeClass('valid').addClass('error');
    }
}

Check Fiddle

Sign up to request clarification or add additional context in comments.

2 Comments

@user2736472.. Removed the 2nd event that is not required. Also changed the event name from onkeyup to keyup
Glad to have helped :)
0

Problem is you are waiting until after user stops typing to bind the keyup event. Since user has stopped typing...this event will never fire.

Try:

// on keydown, start the countdown
$('#contact_phone').keydown(function () {
    var $input= $(this);
    clearTimeout(typingTimer);
    if ($('#contact_phone').val().length == 14) {
        typingTimer = setTimeout(function () {
            doneTyping($input);
        }, doneTypingInterval);
    }
});

//user is "finished typing," do something
function doneTyping($input) {

        if ($input.val().length == 14) {
            $input.removeClass('whiteBorder').removeClass('error').addClass('valid');
        } else {
           $input.removeClass('whiteBorder').removeClass('valid').addClass('error');
        }

}

1 Comment

create a demo in jsfiddle.net

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.