![](/img/trans.png)
[英]How to add text on border with css for input field using React JS?
[英]How to add Validations to my Input Field in React JS
我有一個要添加驗證的輸入字段。 條件應該是amount不能為0,不能輸入任何字符。
我的反應代碼:
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
實際上,您可以使用瀏覽器使用輸入類型提供的默認驗證。 在您的情況下,您的代碼如下:
<input type="number" min="1" value={loanAmount} />
為什么采用這種方法?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.