开发者

Restrict double minus in mui textfiled

开发者 https://www.devze.com 2022-12-07 19:20 出处:网络
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().

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

精彩评论

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