'How to allow only Decimal values up to 2 decimal places in an input field in React JS
I have an Input field to which I want user to be able to enter only number/decimal values up to 2 decimal places. User should not be able to enter any alphabet. Can someone please help me with this
My React Component:
import React from 'react'
import { useState } from "react";
function LoanCalc() {
const [amount, setAmount] = useState();
const handleChange = (event) => {
}
return (
<div className="Container">
<div className="LoanAmount">
<label>Loan Amount:</label>
<span>$
<input type="text" min="4000" max="100000" value={amount} onChange={handleChange}/>
</span>
</div>
</div>
);
}
export default LoanCalc
Solution 1:[1]
check this codesandbox example
https://codesandbox.io/s/xo2x1qnw0w
additional to not restricting any invalid input this will round off if there is more decimal point
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ASHMIL |
