-6

How can I make to accept only numbers inputs in my code: -->> Here ?

Thank you !

Jack25
  • 11
  • 3

1 Answers1

3

You can do this:

<input type="number">

or this:

<input type="text" pattern="[0-9]+">

These will only work in HTML5 compatible browsers.

In javascript you can do this:

JSFiddle

HTML:

<input type="text" id="test" name="test"/>

CSS:

var input = document.getElementById("test");

input.oninput = function(e){
    if (/\D/g.test(this.value))
    {
        // Filter non-digits from input value.
        this.value = this.value.replace(/\D/g, '');
    }
}
Albzi
  • 15,431
  • 6
  • 46
  • 63