30

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.

Ian Kemp
  • 28,293
  • 19
  • 112
  • 138
Mo3z
  • 2,138
  • 7
  • 21
  • 29

4 Answers4

47

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)}/>
Sam
  • 651
  • 5
  • 5
45

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)}
      />
    )
  }
}

Updated based on comment below that errorText is replaced by helperText

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"
            helperText= {this.state.errorText}
            onChange={this.onChange.bind(this)}
          />
        )
      }
    }
Tejasvi Hegde
  • 2,694
  • 28
  • 20
Mo3z
  • 2,138
  • 7
  • 21
  • 29
  • 39
    This doesn't work anymore. `errorText` got replaced by `helperText` and the bool `error` to show it as error text. – Spenhouet Aug 22 '18 at 09:05
  • 3
    Set error to the TextField doesn't invalidate the input inside TextField, doesn't prevent the form submit as well. It is just an UI behaviour with no real function. – Up209d Nov 30 '18 at 01:36
6

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"
                    />
                     );
  }
Ivan Bitkin
  • 61
  • 1
  • 3
5

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

Larry Maccherone
  • 9,393
  • 3
  • 27
  • 43