Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to invalidate a TextField in Material UI

I am using TextField component to capture phone number. As the user is typing, I want to invalidate the entry if it is not a number or if it does not follow a format and display the errorText. Currently errorText is displayed even without touching the field. How can I achieve this behavior?

Any help is greatly appreciated.

like image 349
Mo3z Avatar asked Mar 09 '16 19:03

Mo3z


People also ask

How do you clear the TextField in material UI?

To control and clear the TextField with a state value, we create a state value with React. useState. Next, set this value as the TextField's value prop and make sure the onChange handler updates this value.

How do I add validation to TextField?

Add a TextFormField with validation logicValidate the input by providing a validator() function to the TextFormField . If the user's input isn't valid, the validator function returns a String containing an error message. If there are no errors, the validator must return null.

How do you make a text field mandatory in material UI?

We can use TextField component in Material UI to show input textbox. import TextField from "@material-ui/core/TextField"; When using the TextField component, we can pass the required attribute to mark the component as required.

How do I import a TextField into material UI?

import TextField from '@material-ui/core/TextField'; // or import { TextField } from '@material-ui/core'; You can learn more about the difference by reading this guide. The TextField is a convenience wrapper for the most common cases (80%).


4 Answers

Extending Larry answer, set errorText to a property in state (errorText in below example). When the value in TextField changes, validate the entry and set the value of the property (errorText) to display and hide the error.

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)}
      />
    )
  }
}
like image 77
Mo3z Avatar answered Oct 05 '22 09:10

Mo3z


As of 0.20.1 you can helperText and error props

<TextField 
    hintText="Phone"
    error ={this.state.errorText.length === 0 ? false : true }
    floatingLabelText="Phone"
    name="phone"
    helperText={this.state.errorText}
    onChange={this.onChange.bind(this)}/>
like image 42
Sam Avatar answered Oct 05 '22 08:10

Sam


Material-UI v3.9.3 working version:

class UserProfile extends React.Component {
  constructor(props) {
    super(props);
    this.state = { helperText: '', error: false };
  }

  onChange(event) {
    if (event.target.value.length > 2) {
      this.setState({ helperText: '', error: false });
    } else {
      this.setState({ helperText: 'Invalid format', error: true });
    }
  }

  render() {
    return (

                   <TextField
                      helperText={this.state.helperText}
                      onChange={this.onChange.bind(this)}
                      error={this.state.error}
                      required
                      id="outlined-required"
                      label="First Name"
                    />
                     );
  }
like image 45
Ivan Bitkin Avatar answered Oct 05 '22 08:10

Ivan Bitkin


if errorText is an empty string "", then it will not be displayed. So, set it to that in getInitialState().

like image 5
Larry Maccherone Avatar answered Oct 05 '22 08:10

Larry Maccherone