HOWTO · React

How to Validate React Form

Learn how to validate controlled React form inputs and show useful field-level feedback.

On this page

A form is easier to complete when it tells people what needs attention and how to fix it. In React, keep each field’s value in component state, check those values against rules that fit your task, and show any resulting messages beside the relevant fields. The rules below are examples; adapt them to your product’s requirements.

Choose Validation Rules for the Form

Decide what makes each value acceptable before writing the event handlers. This example requires a name with more than two trimmed characters and an email containing @. That is only a small demonstration: a real application may need different name conventions, stronger email checks, or additional fields. Avoid rejecting valid input based on assumptions about how people format their names or addresses.

Store Input Values with Controlled Components

A controlled input receives its current value from React state and updates that state from its onChange event. Keep the name and email in one values object so updating one field preserves the other. Initialize both values to blank strings, then connect each input’s value and onChange to the corresponding property. This keeps what the user sees synchronized with the data that validation will inspect.

In a class component, this.setState can update one property while retaining the rest of the state. In a function component, use useState and update the object with a copy of its previous value. For example, initialize it with const [values, setValues] = useState({ name: "", email: "" }), then bind the name input with value={values.name} and onChange={event => setValues(previous => ({ ...previous, name: event.target.value }))}. Use the same pattern for email. In either style, use the event’s target.value for the field being edited; do not replace the other field’s value with an old or missing value.

Validate Values and Handle Form Submission

Keep the checks in a validate() function that returns an object keyed by field name. An object with no properties means the values pass these illustrative rules; otherwise, each property contains the message for that field. Call it from handleSubmit(), prevent the browser’s default form submission, and stop the success path when the returned object has keys. In React, save that object in state so rendering can display it.

This standalone JavaScript example validates a complete pair of values and prints a success message when they pass. Run this example:

function validate(values) {
  const errors = {};
  if (values.name.trim().length <= 2) {
    errors.name = "Enter a name longer than two characters.";
  }
  if (!values.email.includes("@")) {
    errors.email = "Enter an email address containing @.";
  }
  return errors;
}

const values = { name: "Jordan Lee", email: "jordan@example.com" };
const errors = validate(values);
const isValid = Object.keys(errors).length === 0;
if (isValid) {
  console.log("Ready to submit");
}
Ready to submit

This example is a plain JavaScript validation helper, not a rendered React component. In your form’s onSubmit handler, pass the current state values to validate(). Store its returned errors, then continue with the valid submission flow only when there are none. The example’s checks are illustrative and do not establish that an address exists or can receive mail.

Display Field-Level Error Messages

Render each message next to its input, using the matching key such as errors.name or errors.email. A conditional paragraph can display a message only when one exists, with an accessible association such as aria-describedby connecting the input to its error text. Give each field a stable id and use that id for the message element. If validation runs after submission, update the errors state on every later edit or revalidation so corrected values clear their old messages. Keep the message specific: tell the user what to change, not merely that a value is wrong.

Understand Client-Side Validation Limits

Client-side checks provide immediate feedback, but users can bypass browser code or send requests directly. Validate submitted data on the server as well, and apply authorization and other protections there. These example rules improve the form interaction; they do not by themselves make an application secure.