简体   繁体   English

Label 与 MaterialUI 数字文本输入字段值重叠

[英]Label overlaps MaterialUI numeric Text Input Field value

I have this Material UI Text Input Field我有这个 Material UI 文本输入字段

<TextField
  label="Numeric"
  inputProps={{ inputMode: "numeric", pattern: "[0-9]+" }}
  type="number"
  variant="standard"
/>

When I type the letter "e" in the Text Input Field:当我在文本输入字段中键入字母“e”时:

  1. Is taking the letter "e" as a valid value I can type within the Text Input Field将字母“e”作为我可以在文本输入字段中键入的有效值
  2. The label overlaps the value label重叠值

常规数值

标签与值重叠

Reproduce error: https://codesandbox.io/s/basictextfields-demo-material-ui-forked-m14u7i?file=/demo.js重现错误: https://codesandbox.io/s/basictextfields-demo-material-ui-forked-m14u7i?file=/demo.js

e is allowed because you can specify large numbers with it. e 是允许的,因为您可以用它指定大数。 you can prevent any character by regex.您可以通过正则表达式阻止任何字符。 this is my solution to prevent user to type e这是我防止用户键入 e 的解决方案

  function preventNonNumericalInput(e) {
    e = e || window.event;
    var charCode = typeof e.which == 'undefined' ? e.keyCode : e.which;
    var charStr = String.fromCharCode(charCode); 
    var pattern = new RegExp(/^[0-9\b]+$/);
    if ( !pattern.test(charStr)) e.preventDefault();
  }

you can add this function to onkeypress您可以将此 function 添加到 onkeypress

    onKeyPress={e => preventNonNumericalInput(e)}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM