I have a question trying to have the input field auto-add a comma on input for readability. E.g 123,456.
.toLocaleString() has been used to resolve the end result, but i need the comma displaying at the time of input also.
Thanks.
const handleGrossIncomeChange = (event) => {
const { name, value } = event.target;
setGrossIncome(value);
if (resultShower === false){
setResultShower(!resultShower);
resultExport = resultShower;
}
else {
return resultShower
}
};
<div className="inputClass">
<div className="input-group">
<p>Gross Income {""}
<span className="currency-input">
<span className="currency-symbol">£</span>
<input
type="number"
name="grossIncome"
id="grossIncome"
autoComplete="off"
onChange={handleGrossIncomeChange}
className="currency-value"
placeholder="0.00"
/>
</span>
</p>
</div>
<button type="submit" name="resultTester" onClick={handleClick}>click me </button>
2
Answers
You can use Intl.NumberFormat Api like below:
Since the input value contains "," (not a number), the input has to be of the text type.
(Tested in React v18.0, ES5)