有一個任務來監督輸入。必須提供僅在物體輸入中輸入數字字串 ([0-9]) 的能力。同時,如果輸入了其他內容,則不要覆寫值,也不要在輸入中顯示錯誤的輸入。我找不到適合我的情況的解決方案。有效性檢查( target.validity.valid )也不起作用,因為我可以控制最小和最大長度。同時,我有幾個輸入的通用功能,但只需要檢查物體。請告訴我如何正確實施對輸入 [0-9] 的檢查,以免輸入其他內容。
資源上的例子不適合,因為沒有考慮最小-最大長度的控制
下面是一個縮短的代碼示例
const [inputState, setInputState] = useState({title : "", entity: ""})
const handleChangeInputValue = (event) => {
const { target } = event;
const { name, value } = target;
// Need to check for numbers
setInputState({ ...inputState, [name]: value });
};
<input
required
minLength={5}
type="text"
placeholder="Enter name"
name="title"
value={inputState.title}
onChange={handleChangeInputValue}
/>
<input
required
minLength={13}
maxLength={15}
type="text"
placeholder="Enter entity"
name="entity"
value={inputState.entity}
onChange={handleChangeInputValue}
/>
uj5u.com熱心網友回復:
你可以使用 HTML 5
<input type="number" name="someid" />
這僅適用于 HTML5 投訴瀏覽器。確保您的 html 檔案的 doctype 是:
<!DOCTYPE html>
uj5u.com熱心網友回復:
if(name==='entity' && !value.match(/^\d $/)) {
return
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/513402.html
