Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid Decimal values in input type number

Tags:

html

How to avoid Decimal values from input of Number in HTML5. Currently it allows user to type decimal value.

like image 893
Kumar Avatar asked May 05 '16 06:05

Kumar


People also ask

How do you restrict a dot in input type number?

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 restrict the number of decimal places in C++?

The C++ setprecision can also be used to format only the decimal places instead of the whole floating-point or double value. This can be done using the fixed keyword before the setprecision() method.


1 Answers

An alternative to the supplied answers is to monitor the keypress while in the input. I personally like leaving the type="number" as an attribute. Here's a JSFiddle

<form action="#" method="post">   Numbers: <input name="num"                    type="number"                   min="1"                   step="1"                   onkeypress="return event.charCode >= 48 && event.charCode <= 57"                   title="Numbers only">   <input type="submit"> </form> 
like image 185
kneeki Avatar answered Sep 23 '22 02:09

kneeki