Is there a way to stop manual input in a type=number but still allow changes with the up/down buttons?
Is there any reason you are avoiding using a drop down select
tag?
This will give the user a very limited choice of numbers (set to your preference).
You could even populate the <option>
fields with numbers 1 through 100 (or whatever you choose) using PHP or JavaScript so you didn't have to manually type each number in the HTML code.
<select>
<option value='1'>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
</select>
Disable writing in input type number HTML5
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.
How to make input type number un-editable but still functioning?
hello I have a working code but i think this is not the best way. I merged the answer from
How to disable backspace if anything other than input field is focused on using jquery
and the answer of TuomasK
here is the working code.
http://jsfiddle.net/vta96mp1/1/
HTML
<input class='testInput' id ='testid' value = '1' name='testname' type='number' min ='1'>
JS
$("#testid").keypress(function (evt) {
evt.preventDefault();
});
$(document).keydown(function(e) {
var elid = $(document.activeElement).hasClass('textInput');
console.log(e.keyCode + ' && ' + elid);
//prevent both backspace and delete keys
if ((e.keyCode === 8 || e.keyCode === 46) && !elid) {
return false;
};
});
How can I disable keyboard input on an html input of type number? What about in react or material-ui?
This seems like a basic question, but I was surprised to not be able to find the answer anywhere, so I'm providing one here.
The key in both cases is to capture the onkeydown
event and run its method preventDefault()
.
Vanilla HTML and javascript
<input type="number" min="0" max="100" step="2" value="10"
onkeydown="preventKeyboardInput(event)" />
<script>
function preventKeyboardInput(event) {
event.preventDefault();
}
</script>
React
<input
type="number"
min={0}
max={100}
defaultValue={10}
step={2}
onKeyDown={(event) => {
event.preventDefault();
}}
/>
Material-UI
Material-UI's <TextField>
is a wrapper around react's <input>
, so all you need to do is pass onKeyDown
down via inputProps
, which you are already using to pass min
, max
, and defaultValue
.
const [value, setValue] = useState(10);
return (
<React.Fragment>
<TextField
name="name"
type="number"
label="label"
inputProps={{
min: 0,
max: 100,
step: 2,
onKeyDown: (event) => {
event.preventDefault();
},
}}
defaultValue={value}
onChange={(event) => {
setValue(Number(event.target.value));
}}
/>
</React.Fragment>
);
UPDATE: I've discovered roundabout that this breaks input boxes on touch devices, or at the very least iOS and iPadOS, neither of which display or allow input from spinners, which is probably why it was downvoted in the first place. Caveat emptor for anyone wanting to use this method.
Remove increment and decrement icon from input field
Hidden in the comments in the post you linked is a suggestion to use the tel
input type:
<input type="tel" value="111">
A few suggestions regarding the number
type:
The number
input type accepts min
and max
attributes to set number constraints.
<input min="1" max="10" type="number" value="1">
HTML Number Input: Only Allow Arrow Buttons
Use event.preventDefault()
in keydown
event;
// no keyboard
document.getElementById("timer02_min").addEventListener("keydown", e => e.preventDefault());
// allow up/down keyboard cursor buttons
document.getElementById("timer02_min2").addEventListener("keydown", e => e.keyCode != 38 && e.keyCode != 40 && e.preventDefault());
no keyboard:
<input type="number" min="00" max ="99" id="timer02_min"
maxlength="2" value="00">
<br>
with up/down cursor keys:
<input type="number" min="00" max ="99" id="timer02_min2"
maxlength="2" value="00">
Is there a way to ONLY allow input using the spinner controls on an input type=number?
You can use Javascript onkeydown
event here... Which will prevent the user to type anything, and still he will be able to use the arrow controls to increase and decrease the numbers.
<input type="number" min="0" value="0" step="5" onkeydown="return false" />
Demo
Note: Just don't depend on JavaScript and HTML, always have a server side validation to ensure that user didn't posted any malicious input. Javascript can be disabled and user can misuse by adding any text in the textbox, but there is no other way you can stop him, so keep a server side check as well.
As you commented that you will like to disable the text selection as well, so that users don't get confused, you can also use CSS positioning techniques here, as you said that intended users are of Chrome only, so there is not much of cross browser issue, so you can do something like this...
Demo 2
Wrap the input
element with the span
element, and just with CSS positioning technique and :after
pseudo, we overlay a virtual element over the input
.
Now I've kept the outline just for the demonstration purposes, you can remove them safely.
span {
position: relative;
outline: 1px solid #00f;
}
span:after {
content: "";
width: 100%;
height: 100%;
left: 0;
top: 0;
position: absolute;
outline: 1px solid red;
width: 91%;
}
Prevent user from typing in input at max value
Checking keyup is too late, the event of adding the character has already happened. You need to use keydown. But you also want to make sure the value isn't > 100 so you do need to also use keyup to allow js to check the value then too.
You also have to allow people to delete the value, otherwise, once it's > 100 nothing can be changed.
<input class="equipCatValidation" type="number" />
When using input type="number", change also needs to be on the event list.
$('.equipCatValidation').on('keydown keyup change', function(e){
if ($(this).val() > 100
&& e.keyCode !== 46 // keycode for delete
&& e.keyCode !== 8 // keycode for backspace
) {
e.preventDefault();
$(this).val(100);
}
});
http://jsfiddle.net/c8Lsvzdk/
How to prevent user from typing in text field without disabling the field?
A non-Javascript alternative that can be easily overlooked: can you use the readonly
attribute instead of the disabled
attribute? It prevents editing the text in the input, but browsers style the input differently (less likely to "grey it out")
e.g. <input readonly type="text" ...>
Related Topics
Create a Complex CSS Shape (Speaking Bubble)
HTML Div Width 100% Not Functioning
CSS Media Queries and Size of Images
How to Replace @Media (Max-Width) Using Stylish or Greasemonkey
How Does Bootstrap Work on Full Hd(1920×1080) Mobiles
Vertically Centered Loading Spinner Overlay
Ie Uses Courier Font When "Font-Family: Monospace" Is Used
Center Image and Text at Large Width, Allow Both to Shrink at Small Width
How Override Material UI Style with Hooks
CSS How to Properly Use Ems Instead of Pixels
How to Horizontally Center-Crop Div Inside Other Div
Svg Attributes Beaten by CSS&Style in Priority
Bootstrap: Horizontal Fields Inside Vertical Form
How to Use Image as a Table Background in Email
Simulating Position: Fixed in Ie6 with a Div of 100% Height