2

In chrome, when we use <input type='number'>, if we type any non numeric characters, they are not displayed.

In mozilla, they are displayed. However, when I get e.target.value, I am getting an empty string with a validation message Please enter an number.

I want the chrome functionality in mozilla - I am trying to strip out the last character if it is not numeric - but as the value is empty string as soon as non numeric character is entered I am unable to do so.

user2133404
  • 1,816
  • 6
  • 34
  • 60

2 Answers2

1

You could do something like this: http://jsfiddle.net/zpg8k/1660/

$('input[type="number"]').keypress(function(e) {
    var a = [];
    var k = e.which;

    for (i = 48; i < 58; i++)
        a.push(i);

    if (!(a.indexOf(k)>=0))
        e.preventDefault();
});
justtry
  • 639
  • 4
  • 8
1

document.querySelector("input").addEventListener("keypress", function (evt) {
    if(evt.which == 8){return} // to allow BackSpace
    if (evt.which < 48 || evt.which > 57)
    {
        evt.preventDefault();
    }
});
Number: <input type='number'>
Muhammad
  • 6,725
  • 5
  • 47
  • 54