如何在Material UI中使TextField无效

Mo3z:

我正在使用TextField组件捕获电话号码。在用户键入时,如果该输入不是数字或未遵循格式并显示errorText,则我想使该输入无效。当前,即使不触摸该字段也会显示errorText。我该如何实现这种行为?

任何帮助是极大的赞赏。

Mo3z:

扩展Larry答案,将errorText设置为状态下的属性(以下示例中的errorText)。当TextField中的值更改时,验证条目并设置属性(errorText)的值以显示和隐藏错误。

class PhoneField extends Component
  constructor(props) {
    super(props)
    this.state = { errorText: '', value: props.value }
  }
  onChange(event) {
    if (event.target.value.match(phoneRegex)) {
      this.setState({ errorText: '' })
    } else {
      this.setState({ errorText: 'Invalid format: ###-###-####' })
    }
  }
  render() {
    return (
      <TextField hintText="Phone"
        floatingLabelText="Phone"
        name="phone"
        errorText= {this.state.errorText}
        onChange={this.onChange.bind(this)}
      />
    )
  }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

React如何在Material-UI中有条件地覆盖TextField错误颜色?

如何屏蔽我的Material-UI TextField?

如何在不使用MUIThemeProvider的情况下覆盖material-ui TextField组件的样式?

如何在React中使用Material UI图标?

如何从Material UI获取TextField的输入值?

如何在React中使用Material UI组件

如何在不使用<MuiThemeProvider />的情况下更改Material-UI TextField底部颜色

如何在错误和焦点上更改Material-UI TextField底部和标签颜色

如何在React中使用Material-UI在onFocus事件上的Textfield中选择文本的一部分?

如何在不使用onChange事件的情况下从Material UI获取TextField的输入值?

如何在React Material UI TextField中将占位符和文本居中

如何在Material-UI中使用framer-motion?(reactjs)(@ material-ui / core)(成帧器运动)

使用TypeScript和Formik反应如何在Textfield Material-UI中使用图标

如何在Material UI选择菜单中使用SubHeader

如何在Material UI中删除筹码

如何在ReactJS Material UI中使用useStyle?

如何在div中使用Material UI主题颜色?

如何在电子中使用Material-ui

Material UI:如何在头像中使用我自己的图像

如何在 Material UI 中使用 Grid direction="row"

如何在material-ui-dropzone中使用initialFiles

如何在formik中使用material-ui选择字段?

rectjs: material ui: TextField: 如果 type 和 min 无效则显示错误

Material-UI:如何在 datetime-local TextField 中禁用以前的日期

如何在 Material UI TextField 组件中的关闭图标上添加自定义处理程序?

如何在 Material-UI 中使图像响应

如何在@material-ui 中使用 useStyles 编写 !important

如何在 Material UI makestyles 中使用媒体查询

如何在 Material UI 中将按钮附加到 TextField