The following code is intended to check if 4 numbers are entered in the blurred field. If not, the field value is deleted, and the field is focused. The deletion works fine, but the the call to focus() does not work.
$('input.dateValue').live('blur',function(event){
if (!(/(\d){4}$/.test($(this).attr('value')))) $(this).attr('value','').focus();
});
Why does the call to focus() not focus the field?
Since the
blurevent fires before the actual loss of focus, you cannot use.focus()right away. You have to push it down the stack, so that it executes after theinputhas lost focus. Put your.focus()in a timer (no delay necessary):Here’s the fiddle: http://jsfiddle.net/TdfFs/
Update: to demonstrate that this does work in Chrome, I made another fiddle: http://jsfiddle.net/TdfFs/1/