0

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

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>
        );
    }
}
// -----------------------------------------------------------------------------------------------------------------
//  I N T E R N A L
// -----------------------------------------------------------------------------------------------------------------

// =====================================================================================================================
//  E X P O R T
// =====================================================================================================================
export default Main;

1 Answer 1

1

Using a react component state, one can store the TextField value and use that as an indicator for an error. Material-UI exposes the error and helperText props to display an error interactively.

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!' : ' '}
/>
Sign up to request clarification or add additional context in comments.

2 Comments

Works fine,but it is displaying the error message even before the user inputs anything.How could i fix that?
for this you can set the value of error as a useState hook and then onBlur method of TextField, set the useState hook value ( it's just an idea of how it will work ). Please vote and accept the answer so that this can be closed. thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.