1

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?

2
  • 1
    This is opinion based, but I think probably not. Although it would be more efficient so no information is missed, and you can get back to it quick, it will probably annoy some users. Just leave a notification, and let THEM decide what to do.
    – alexr101
    Jan 6, 2017 at 0:49
  • @alexr101 thanks for your comment. I decided to just leave a notification and let the user decide what to do.
    – Pablo
    Jan 6, 2017 at 1:17

2 Answers 2

1

In my opinion:

  1. Don't steal focus

  2. Don't prevent user from entering any input e.g. copying and pasting, then correcting

  3. Validate immediately, don't wait for loss of focus

Reasons:

  1. User will eventually end up trying to enter input elsewhere and have the input reach the wrong place or get lost

  2. User may try to copy a number from some place that also selects the spaces e.g. Word, or try to enter an input that is initially invalid e.g. "-" then add 1 and reach a valid integer input "-1". (Same with floating numbers, where 1e is invalid, however 1e3 is valid (=1000)).

  3. Give users early, not intrusive, feedback while his/her attention is on the relevant input field. E.g. a red comment after the input field.

1
  • thank you for your comments. Specially number 2 makes a lot of sense, I've wasted so many hour trying to allow only the specific characters and then you forget to look up for arrow keys or control keys.
    – Pablo
    Jan 7, 2017 at 23:15
-1

Avoid any extra interactions from the moment the incorrect data is being entered and the user realises it. Inform the user of valid and/or invalid characters before and while he is typing.

  • Don't let the user enter any invalid character in the first place.
  • If the former is not possible inform the user of any invalid character he is introducing as he types.

If you can't do the above and you can only inform about the invalid charaxter after the user focuses out of the field, let him him know but don't trigger interactions in the users behalf. Let him decide to focus back as soon as he reads the message or later.

4
  • This would prevent users from copying " 123 " and pasting it into the box, then correcting. This is a very annoying behaviour. Jan 7, 2017 at 13:50
  • @DannyVarod is the space in " 123 " intended? If not, why would it prevent it?
    – Alvaro
    Jan 7, 2017 at 14:03
  • When user controls filter out invalid characters e.g. characters outside the range of 0..9, then pasting " 123 " or "123 " is blocked by the controls. I've experienced this abuse in a various apps many years ago. Jan 7, 2017 at 14:41
  • @DannyVarod Well, in the first bold scenario I suggest the form would prevent the characters and tell the user: your string ' 123' contains an invalid character: ' '. Maybe in this case, but I'm not so sure. Anyway, thanks for the feedback.
    – Alvaro
    Jan 7, 2017 at 14:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.