1

I have one textbox coded like below :

<input name="txttelephone" type="text" maxlength="16" id="txttelephone" class="InputTextBox" onkeypress="return NumbersOnly(event);" required />

And javascript function is like below :

function NumbersOnly(e) {
    var unicode = e.charCode ? e.charCode : e.keyCode;
    if (unicode != 8) {
        if (unicode < 48 || unicode > 57) {

            if (unicode == 9)
                return true;
            else
                return false;
        }
    }
}

Now When I run this in chrome arrow keys working proper but in firefox arrow key is not working. Not getting what is the issue.

Please help me with this.

Thanks,

Dipa

iDipa
  • 347
  • 2
  • 9
  • 20
  • Refer to following page for [Char - keyCodes](http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes). You are returning true only for number keys above qwerty keyboard. This will not even support num pad. – Rajesh Dec 26 '15 at 10:02
  • Maybe this page can help you: http://stackoverflow.com/questions/5597060/detecting-arrow-key-presses-in-javascript – The fourth bird Dec 26 '15 at 10:04
  • Why you don't use input type number? – jcubic Dec 26 '15 at 10:05
  • @jcubic input type number allows you to enter string. Also it does not have `maxLength` property. – Rajesh Dec 26 '15 at 10:07
  • Refer following [Post](http://stackoverflow.com/questions/32439723/keydown-event-issue-on-html-textbox). – Rajesh Dec 26 '15 at 10:08
  • @Rajesh you can type only `e` because it can be part of float point number https://jsfiddle.net/44e5e3h0/ – jcubic Dec 26 '15 at 10:11
  • @jcubic My apologies then. But still I'd recommend using input type="tel". Number adds spinner features which might cause issues for mobile number field and you will have to write extra css just to hide it – Rajesh Dec 26 '15 at 10:15

1 Answers1

0

You have to exclude arrow key codes. Try following modification in your code.

function NumbersOnly(e) {
    var unicode = e.charCode ? e.charCode : e.keyCode;
    if (unicode != 8) {
        if (unicode < 48 || unicode > 57) {

            if (unicode == 9 || IsArrows(e) )
                return true;
            else
                return false;
        }
    }
}
function IsArrows (e) {
       return (e.keyCode >= 37 && e.keyCode <= 40); 
}
Amol Patil
  • 985
  • 2
  • 11
  • 43