Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable writing in input type number HTML5

Tags:

html

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>
like image 693
Souad Avatar asked Jun 18 '13 08:06

Souad


People also ask

How do I disable editing in input tag?

The readonly attribute can be set to keep a user from changing the value until some other conditions have been met (like selecting a checkbox, etc.). Then, a JavaScript can remove the readonly value, and make the input field editable.

How do you block e in input type number in react JS?

To prevent e and dot in an input type number with React, we can validate the input value in the onChange prop function. const C = () => { //... const [val, setVal] = useState(""); return ( <div className="App"> <input type="text" value={val} onChange={(e) => setVal(e. target.

How do I turn off the input arrow in numbers?

Using inputmode=”numeric” attribute you can find an input box without an arrow.


3 Answers

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.

like image 89
Ilu Avatar answered Nov 03 '22 18:11

Ilu


You can cancel KeyDown event using this

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

like image 26
Viktor Tarnavskyi Avatar answered Nov 03 '22 16:11

Viktor Tarnavskyi


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>
like image 30
vipatron Avatar answered Nov 03 '22 18:11

vipatron