Let's say I have a text box where you only are allowed to enter integers. When the focus is lost, then the page should check whether the entry is an integer or not. If it's not an integer, then the user should get an error message. Is it a good idea to force focus back on the text box after the user closes the alert box?
On one project of mine, I have:
jQuery("input[data-type=int]").blur(function() {
var val = this.value.trim();
var i = parseInt(val);
if(isNaN(i))
{
alert("Please enter a number");
this.focus();
}
});
On an older version of google chrome the alert box appears and after clicking on the "OK" button, the text box got focus again. On a newer version of google-chrome the blur
event keeps getting fired once this.focus()
is executed which spawns alert boxes without end.
I tried the same page with firefox and there the behaviour was different, this.focus()
gets executed but the text box doesn't get the focus. At this point I'm questioning whether this strategy is good or not. What would you do?