Anuj
Anuj

Reputation: 1526

control not getting focus after required field validation alert

i am having two textboxes and first is mandatory,when i press tab an after leaving text box empty alert comes properly,but problem is that when i set focus to the control it doesn't work,focus goes to next control because i press tab.

this is what i have written

$("#amount").on("blur", function () {
    if (!$(this).val()) {
        alert("This field is required");
        $(this).focus();
    }
});

see this fiddle

the above code works fine in crome but it doesn't work in Mozilla

Upvotes: 0

Views: 101

Answers (2)

Cerlin
Cerlin

Reputation: 6722

Slightly improved version from vivek's answer

$("#amount").on("blur", function () {
    var $this = $(this);
    if (!$this.val()) {
        alert("This field is required");
        setTimeout(function(){$this.focus();},1);
    }
});

FIDDLE

Upvotes: 1

vivek
vivek

Reputation: 71

try this

$("#amount").on("blur", function () {
    if (!$(this).val()) {
        alert("This field is required");
        $(this).focus();
        setTimeout( function() { $(window).focus(); $("#amount").focus() })
    }
});

Upvotes: 1

Related Questions