admin管理员组文章数量:1434914
This is my current code.
<TextField error={values[1].error} fullWidth id="id" type="number" value={values[1].Id} placeholder='Enter ID' onChange={handleChange(1,'Id')} variant="outlined" inputProps={{min: 0,inputMode: 'numeric', pattern: '[0-9]'}} onBlur={() => handleOnBlurEvent(1,'Id')} />
This is my current code.
<TextField error={values[1].error} fullWidth id="id" type="number" value={values[1].Id} placeholder='Enter ID' onChange={handleChange(1,'Id')} variant="outlined" inputProps={{min: 0,inputMode: 'numeric', pattern: '[0-9]'}} onBlur={() => handleOnBlurEvent(1,'Id')} />
Share
Improve this question
asked Aug 4, 2022 at 8:07
tosseftossef
111 gold badge1 silver badge2 bronze badges
3
-
2
E.g.
-1e2
is a valid number. – jonrsharpe Commented Aug 4, 2022 at 8:12 - thats becoz e is for exponential, if you want to restrict it then probably you need to have a custom validation using keyup or keydown – Amaarockz Commented Aug 4, 2022 at 8:12
- can you please suggest any documentation to restrict e and dashes? – tossef Commented Aug 4, 2022 at 8:21
1 Answer
Reset to default 2As mentioned by jonrsharpe, numbers containing -
or e
are still valid numbers.
If you still want to limit the changes that are being accepted, you can make use of a custom handleNumberChange
function that filters chars such as e
, E
and -
.
const handleNumberChange = (id: number, key: string, value: string) => {
if (["e", "E", "-"].some((char) => value.includes(char))) return;
// handle change here
};
You would use it like that in your MUI <TextField/>
:
<TextField
...
onChange={(e) => handleNumberChange(1, "Id", e.target.value)}
/>
本文标签:
版权声明:本文标题:javascript - How do I make Mui textfield type number only accept number ? Currently it accept numbers,"e" and 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745623911a2666854.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论