[英]Validate textfield based on input React Material Ui
Helo.你好。 I am looking for a way to set error whenever the value inserted in the first text field is different than "28,71",else set correct
每当第一个文本字段中插入的值不同于“28,71”时,我正在寻找一种设置错误的方法,否则设置正确
My code so far:到目前为止我的代码:
class Main extends React.PureComponent { render() { return ( <Box sx={SX.root}> <Box className="textFieldWrapper"> <TextField sx={SX.textField} id="filled-number" type="number" label={this.props.settings.texts.t1} variant="outlined" size="small" style={{width: '100px'}} /> </Box> <Box className="textFieldWrapper2"> <TextField sx={SX.textField2} id="filled-number" type="number" label={this.props.settings.texts.t2} variant="outlined" size="small" style={{width: '100px'}} /> </Box> </Box> ); } } // ----------------------------------------------------------------------------------------------------------------- // INTE R NAL // ----------------------------------------------------------------------------------------------------------------- // ===================================================================================================================== // EXPO R T // ===================================================================================================================== export default Main;
Using a react component state, one can store the TextField value and use that as an indicator for an error.使用 React 组件 state,可以存储 TextField 值并将其用作错误指示器。 Material-UI exposes the error and helperText props to display an error interactively.
Material-UI 公开了 error 和 helperText 道具以交互方式显示错误。
Take a look at the following example:看看下面的例子:
<TextField
value={this.state.text}
onChange={event => this.setState({ text: event.target.value })}
error={text === ""}
helperText={text === "" ? 'Empty field!' : ' '}
/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.