9951 explained code solutions for 126 technologies


reactjsHow do I validate a form using React.js?


Form validation is an important part of any web application. React.js provides developers with tools to easily validate user input. Here is an example of how to validate a form using React.js:

class Form extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      value: '',
      valid: false
    };
  }

  handleChange(event) {
    this.setState({
      value: event.target.value,
      valid: event.target.value.length > 5
    });
  }

  render() {
    return (
      <form>
        <input type="text" value={this.state.value} onChange={this.handleChange.bind(this)} />
        <p>{this.state.valid ? 'Valid' : 'Invalid'}</p>
      </form>
    );
  }
}

ReactDOM.render(<Form />, document.getElementById('root'));

The example code above will render a form with an input field and a message that will change depending on the validity of the input. The constructor function sets the initial state of the form, with the value and valid properties. The handleChange function is used to update the state of the form when the user types in the input field. Finally, the render function is used to render the form.

The list below explains the parts of the code:

  1. constructor: Sets the initial state of the form.
  2. handleChange: Updates the state of the form when the user types in the input field.
  3. render: Renders the form.

Helpful links

Edit this code on GitHub