开发者

Restrict double minus in mui textfiled

Currently, Number type inputs accepting double minus (i.e: --1). But I want only one - sign. If user press another minus then it should preventDefault().

Found a s开发者_开发技巧olution here. But it has some edge cases like, if user input '-123' and then go back and remove the '-' then user can't give the '-' again.


you can always control the value with onChange if you are using react or a similar framework

<TextField
onChange={(event)=> {
if(event.target.value.toString().search('--') == -1) {setInputValue(event.target.value)}}}
value={inputValue} 
/>

with a local state

const[inputValue,setInputValue] = useState("")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜