I have a Input field to which I want to add validations. The conditions should be that amount cannot be 0 and should not enter any characters.
My React Code:
import React from 'react'
import { useState } from 'react';
function LoanInput() {
const [loanAmount, setLoanAmount] = useState(0);
const handleValidation = (e) => {
const formattedValue = (Number(e.target.value.replace(/\D/g, '')) || 0).toLocaleString();
setLoanAmount(formattedValue);
}
return (
<div className="LoanAmountInput">
<div className="LoanAmount">
<label>Loan Amount</label>
<input type="text" value={loanAmount} onChange={handleValidation} />
<span style={{ color: "red" }}></span>
</div>
</div>
);
}
export default LoanInput