26

I want to disable writing into a field of type number, for avoiding strings data, and write just numbers, so how to enable scroll bars only ?

<form>
 <input type="number"  path="note" min="1" max="20"/>
</form>
Mohammad Kermani
  • 5,188
  • 7
  • 37
  • 61
Souad
  • 4,856
  • 15
  • 80
  • 140
  • two are the same thing, i think what i want to do is clear, input of type number allow just numeric data so enable only scroll bars – Souad Jun 18 '13 at 09:16
  • Can't you just use the `readonly` attribute? It is supported for type number – Ron van der Heijden Jun 18 '13 at 09:33
  • @Souad 'disable writing in input type number HTML5' you must change the heading of your question it is misleading. – NetStarter Jun 18 '13 at 10:13
  • From a UX perspective: Isn't weird that a control appears as active (as we need the arrows), we can focus on the input, but then key strokes are ignored? – tokland Aug 02 '23 at 10:11

10 Answers10

46

If you are able/allowed to use jQuery, you can disable keypress on the type='number'.

$("[type='number']").keypress(function (evt) {
    evt.preventDefault();
});

This allows you to use up and down arrows on the input, but doesn't allow keyboard input.

Ilu
  • 894
  • 6
  • 12
  • 3
    Maybe something changed since '13, now keypress won't work for keys Delete and Backspace, this will all user to delete value, I used keydown that covers all – Vladimir Bozic Aug 17 '17 at 10:46
  • What's the use of disabling the keyboard ? who does it benefit ? If I'm trying to enter text into a number field then it's not probable that I would understand why my keyboard is broken. I hate it when UX "experts" force developers to do that. – Cesar Jan 21 '20 at 11:47
31

You can cancel KeyDown event using this

<input type="number" onKeyDown="return false">

5

If I understood you correctly, I just implemented the same thing myself using vanilla Javascript (no jQuery).

window.onload = () => {
  //add event listener to prevent the default behavior
  const mouseOnlyNumberInputField = document.getElementById("mouse-only-number-input");
  mouseOnlyNumberInputField.addEventListener("keypress", (event) => {
    event.preventDefault();
  });
}
<form>
  <input id="mouse-only-number-input" name="number" type="number" min="1" max="20" value="10" />
</form>
vipatron
  • 166
  • 1
  • 8
1

//using this method can make a disabled writing on input type=number
input {
  cursor: default;
  /*when hover it default pointer*/
  color: transparent;
  /*make blinking cursor disappear*/
  /*but it will make to text disappear*/
  text-shadow: 0px 0px black;
  /*when it disappear add text shadow to black*/
}
<input type="number" min="-32767" max="32767" onkeydown="return false //return value false" value="0">
<!--make an input-->

using this method with css and html to do that

tony
  • 61
  • 3
0

No Scrolling, no increment of numbers, max-length provided, no copy paste.

Check out the Fiddle below, which has the features needed within a type number form field.

HTML

<form class="form-horizontal home" role="form" action="" method="post" id="payment-form" novalidate="novalidate">
<label for="number">Mobile number : </label>
<input class="form-control" id="number" name="number" type="number" data-rule-required="true" aria-required="true" oninput="this.value = this.value.replace(/[^0-9.]/g, ''); this.value = this.value.replace(/(\..*)\./g, '$1');" onKeyDown="if(this.value.length==10 && event.keyCode!=8) return false;">
</form>

CSS

input[type=number]::-webkit-inner-spin-button, 
input[type=number]::-webkit-outer-spin-button { 
  -webkit-appearance: none; 
  margin: 0; 
}

DEMO - JSFIDDLE

Surya R Praveen
  • 3,393
  • 1
  • 24
  • 25
0

//using this method can make a disabled writing on input type=number
input {
  cursor: default;
  /*when hover it default pointer*/
  color: transparent;
  /*make blinking cursor disappear*/
  /*but it will make to text disappear*/
  text-shadow: 0px 0px black;
  /*when it disappear add text shadow to black*/
}
<input type="number" min="-32767" max="32767" onkeydown="return false //return value false" value="0">
<!--make an input-->
0

In the onkeydown event, event.keyCode returns the ASCII code of the pressed key. The code for numeric keys 0-9 are 48-57. So, we can check if the pressed key is a number (between 0-9) and allow the event to proceed or not based on that

 <input type="number" path="note" min="1" max="20" onkeydown="return event.keyCode >= 48 && event.keyCode <= 57"/>
0

In js file :

$("[type='number']").keydown(function (e) {
        return (e.keyCode === 38 || e.keyCode === 40) ? false: true;
    })
ro-hit
  • 1
  • 1
0

Looking for css solutions only:

[type="number"] {
  pointer-events: none;
}

::-webkit-inner-spin-button {
  pointer-events: auto;
  opacity: 1;
}


/* fallback for firefox: */
@supports (-moz-appearance: textfield) {
    [type="number"] {
        pointer-events: unset;
    }
}
<input type="number">
imhvost
  • 4,750
  • 2
  • 8
  • 10
-4

Firstly, your markup is incorrect. The form tags must surround the input tag:

<form name="my_form">
    <input ...code...
    <input ...code...
</form>

Secondly, if you are using HTML5 and want a number input, you can use this:

<input type="number" name="my_number" min="0" max="100" step="1" value="50"/>

(from W3Schools)

Be aware that current browser implementations of the "number" input vary.

You then reference the value using Javascript, or have it posted using the form.

TheTurkey
  • 198
  • 9