简体   繁体   English

反应材料ui文本字段(类型=数字)验证10个字符是否存在

[英]react material ui Textfield (type=number) validate 10 characters are existing

<TextField
    variant="outlined"
    required
    fullWidth
    id="accno"
    label="Main Account No"
    type="number"
    name="accno"
    //inputProps={{ className:"input-acc", pattern: "^.{0,10}$"}}
    autoComplete="accno"
    onChange={(e) => this.handleChange(e)}
    onInput={(e)=>{ 
        e.target.value = Math.max(0, parseInt(e.target.value) ).toString().slice(0,10)
    }}
    min={10}
/>

react material ui Textfield (type=number) validate 10 characters are existing (mandatory).Otherwise i need form validation to show error.反应材料 ui 文本字段(类型=数字)验证 10 个字符是否存在(强制)。否则我需要表单验证以显示错误。 I tried regex pattern it wont work because this field type is number.我尝试了正则表达式模式,它不起作用,因为这个字段类型是数字。

The intuitive way is checking the length of value in handleChange直观的方法是检查handleChange中值的长度

onChange={(e) => {
if(e.target.value.toString().length >= 10){
  //display error
 }
this.handleChange(e)
  }
}

You might also need to add the error property in TextField您可能还需要在 TextField 中添加error属性

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

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