How to invalidate a TextField in Material UI

Mo3z

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.

Mo3z

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to style material-ui textfield

From Java

How get data from material-ui TextField, DropDownMenu components?

From Java

How to change the border color of Material-UI <TextField/>

From Dev

React How to conditionally override TextField error color in Material-UI?

From Dev

material-ui textfield with japanese

From Dev

React Material UI textfield label problem

From Dev

React Material UI TextField Styles Not Working

From Dev

Material UI - Show adornments vertically in TextField

From Dev

React Material UI TextField FormHelperTextProps Styling Not Working

From Dev

How can i apply my own css style in ui Material Textfield?

From Dev

ReactJS - How can I set a value for textfield from material-ui?

From Dev

How can I make TextField in Javafx to look as beautiful as Android or material UI

From Java

Display number of matching search options on Textfield input in Material UI Autocomplete

From Dev

Material-ui TextField messing up with react-redux

From Dev

Material-UI TextField inside Popper inside Dialog not working

From Dev

Call phone or open email client from Material UI TextField

From Dev

JSX syntax for dynamic width of Material UI TextField underline React

From Dev

Material UI doesn't support colored text on multiline textfield (React)

From Java

How to remove indicator line of TextField in Androidx Compose Material?

From Java

In Material-ui when do we use Input vs. Textfield for building a form?

From Dev

Material-UI TextField errorText not appearing when I set state with Object.assign

From Dev

How to reduce Material Toolbar height Material-UI?

From Dev

How to set minimum height on material cards in Material UI?

From Dev

How to apply custom theme in material ui

From Java

How to change padding of Material UI's datepicker?

From Java

How to set max value for LinearProgress in material ui?

From Dev

material ui SelectField how to change width of Menu

From Dev

How to create Material UI component in Om Clojurescript?

From Dev

How to align Material buttons with other UI elements

Related Related

  1. 1

    How to style material-ui textfield

  2. 2

    How get data from material-ui TextField, DropDownMenu components?

  3. 3

    How to change the border color of Material-UI <TextField/>

  4. 4

    React How to conditionally override TextField error color in Material-UI?

  5. 5

    material-ui textfield with japanese

  6. 6

    React Material UI textfield label problem

  7. 7

    React Material UI TextField Styles Not Working

  8. 8

    Material UI - Show adornments vertically in TextField

  9. 9

    React Material UI TextField FormHelperTextProps Styling Not Working

  10. 10

    How can i apply my own css style in ui Material Textfield?

  11. 11

    ReactJS - How can I set a value for textfield from material-ui?

  12. 12

    How can I make TextField in Javafx to look as beautiful as Android or material UI

  13. 13

    Display number of matching search options on Textfield input in Material UI Autocomplete

  14. 14

    Material-ui TextField messing up with react-redux

  15. 15

    Material-UI TextField inside Popper inside Dialog not working

  16. 16

    Call phone or open email client from Material UI TextField

  17. 17

    JSX syntax for dynamic width of Material UI TextField underline React

  18. 18

    Material UI doesn't support colored text on multiline textfield (React)

  19. 19

    How to remove indicator line of TextField in Androidx Compose Material?

  20. 20

    In Material-ui when do we use Input vs. Textfield for building a form?

  21. 21

    Material-UI TextField errorText not appearing when I set state with Object.assign

  22. 22

    How to reduce Material Toolbar height Material-UI?

  23. 23

    How to set minimum height on material cards in Material UI?

  24. 24

    How to apply custom theme in material ui

  25. 25

    How to change padding of Material UI's datepicker?

  26. 26

    How to set max value for LinearProgress in material ui?

  27. 27

    material ui SelectField how to change width of Menu

  28. 28

    How to create Material UI component in Om Clojurescript?

  29. 29

    How to align Material buttons with other UI elements

HotTag

Archive