0
<input id='nb' name="nb" type="number" placeholder="Number"/>

jQuery.val() returns the value if there are only numbers, but empty if there is any letter.

Chrome shows the input number arrows (and they work), but I can type letters too.. Weird thing

BTW on mozilla and IE8 it becomes a normal input text (guess I have an old mozilla)

Any idea ? Couldn't find anything on jQuery doc specific to number inputs with .val()

10
  • Out of curiosity, what does this.value give (in place of $(this).val())? Commented May 22, 2013 at 10:39
  • 1
    The most easy way to do this, in approach, is to ignore HTML5, use an <input type="text"... and set an .onkeyup function to accept only numbers (regex for example), because HTML5 it's not really supported by all. Commented May 22, 2013 at 10:39
  • Change type to text, you will get number as well as letters whatever you type Commented May 22, 2013 at 10:40
  • It's similar, but not duplicate. Here, OP is using HTML5. Duplicated will be if he worked as I suggested in a previous comment and his real problem is getting the value from the input. Commented May 22, 2013 at 10:42
  • If Chrome allows to input random letters as well, that’s a bug in my opinion – w3.org/TR/html5/forms.html#number-state-(type=number) says, “User agents must not allow the user to set the value to a non-empty string that is not a valid floating-point number.” Commented May 22, 2013 at 11:35

2 Answers 2

1

Suggestion for input type="text" instead HTML5 input input type="number":

//HTML

<input id="inp" type="text" />

//JAVASCRIPT (JQUERY NEEDED)

function onlyNumbers(evt) {
    // SOME OPTIONS LIKE ENTER, BACKSPACE, HOME, END, ARROWS, ETC.
    var arrayExceptions = [8, 13, 16, 17, 18, 20, 27, 35, 36, 37,
        38, 39, 40, 45, 46, 144];
    if ((evt.keyCode < 48 || evt.keyCode > 57) &&
            (evt.keyCode < 96 || evt.keyCode > 106) && // NUMPAD
            $.inArray(evt.keyCode, arrayExceptions) === -1) {
        return false;
    }
}

$('#inp').on('keydown', onlyNumbers);

//JAVASCRIPT (WITHOUT JQUERY)

function inArray(value, arr) {
    for(var i = 0; i < arr.length; i++) {
        if (value === arr[i]) {
            return i;
        }
    }
    return -1;
}

function onlyNumbers(evt) {
    // SOME OPTIONS LIKE ENTER, BACKSPACE, HOME, END, ARROWS, ETC.
    var arrayExceptions = [8, 13, 16, 17, 18, 20, 27, 35, 36, 37,
        38, 39, 40, 45, 46, 144];
    if ((evt.keyCode < 48 || evt.keyCode > 57) &&
            (evt.keyCode < 96 || evt.keyCode > 106) && // NUMPAD
              inArray(evt.keyCode, arrayExceptions) === -1) {
        return false;
    }
}

document.getElementById('inp').onkeydown = onlyNumbers;

As I said in my comment, until HTML5 becomes an standard web language that we can work with it properly, I prefer to use this.

Demo with jQuery
Demo without jQuery

Sign up to request clarification or add additional context in comments.

Comments

0

If you want to get only numbers from input then you need to use regular expressions. Initially get the value from input using $(your_element).val() then use regular expressions.

Here is a running example. Hope it helps.

1 Comment

This is not my problem, I am already using this kind of solution to enable cross browser support. I hoped that on HTML5 capable browsers, this wouldn't be needed, but apparently even chrome doesn't always support "number" inputs. Just for info, what I use jsfiddle.net/gJQCf

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.