1

jsfiddle: http://jsfiddle.net/ztNHL/3/

Select the top input, and then tab to the second. It will put your cursor at the end of the value, rather than selecting all. if you remove the javascript and run again, you can see this behavior doesnt happen.

Clueless.

elzi
  • 5,442
  • 7
  • 37
  • 61

4 Answers4

2

There is nothing strange. keyup event reacts on TAB and executes your code

this.value = this.value.replace(/[^0-9\.]/g, "");

witch updates the value of the input field. That's why it first selects the field and then removes the selection.

VisioN
  • 143,310
  • 32
  • 282
  • 281
  • I noticed my error and changed it right after to keydown and it fixes... doh. But there are other problems with it that were solved by other in the post. Thank you, though! – elzi Jul 05 '12 at 22:30
2
$(function() {
    $('.numbersOnly').keyup(function (evt) {
        if ($.inArray(evt.which, [37, 38, 39, 40]) != -1) return false;
            this.value = this.value.replace(/[^\d\.]/g,'');
            if (evt.which == 9) this.select(); //<-- re-select field value
    });
});            
​

Some other modifications I made:

  • pattern made shorter (using \d alias)
  • cursor keys are re-enabled (in your code they were blocked from working)
  • the selection happens only if the tab key was pressed, not merely any key
Mitya
  • 33,629
  • 9
  • 60
  • 107
2

Hiya there are few other issues like:

Working demo Smooth: http://jsfiddle.net/wWxVL/2/

  • The left arrow keys don't work.
  • The stuff is bit quirky when user type.
  • Tab/select all issue (known)

An awesome post here: How to allow only numeric (0-9) in HTML inputbox using jQuery?

Hope it helps, cheers

code

$(document).ready(function() {
    $(".numbersOnly").keydown(function(event) {
        // Allow: backspace, delete, tab, escape, and enter
        if (event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 27 || event.keyCode == 13 ||
        // Allow: Ctrl+A
        (event.keyCode == 65 && event.ctrlKey === true) ||
        // Allow: home, end, left, right
        (event.keyCode >= 35 && event.keyCode <= 39)) {
            // let it happen, don't do anything
            return;
        }
        else {
            // Ensure that it is a number and stop the keypress
            if (event.shiftKey || (event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105)) {
                event.preventDefault();
            }
        }
    });
});​
Community
  • 1
  • 1
Tats_innit
  • 33,991
  • 10
  • 71
  • 77
  • I went with Utkanos' solution, though this way taught me a lot and works just as well. Thank you very much. – elzi Jul 05 '12 at 22:29
1

When I tab (in chrome) to the second box, it doesn't go to the end of the value until i let go of the tab key(i.e. on keyup)

this.value = this.value.replace(/[^0-9\.]/g,'');

is being executed when you let go of the tab key

Isaac Fife
  • 1,659
  • 13
  • 15
  • Yeah, I'm a moron. keydown fixes, but there are other problems, as addressed by the other posters. – elzi Jul 05 '12 at 22:31