Skip to main content

Form Validation

Learn how to implement client-side and server-side form validation in React Router applications.

Overview

React Router provides a robust form handling system through the <Form> component and action functions. You can implement validation at multiple levels:
  • Client-side validation for immediate feedback
  • Server-side validation in action functions
  • Progressive enhancement for JavaScript-disabled environments

Server-Side Validation

Validation in action functions ensures data integrity regardless of client-side state:

Client-Side Validation

Add immediate feedback using HTML5 validation or custom logic:

Using Validation Libraries

Integrate popular validation libraries like Zod:

Field-Level Validation

Validate individual fields as users type:

Preserving Form State

Keep user input after validation errors:

Best Practices

  1. Always validate on the server - Client-side validation can be bypassed
  2. Provide clear error messages - Tell users exactly what’s wrong and how to fix it
  3. Use semantic HTML - required, minLength, pattern attributes provide free validation
  4. Consider accessibility - Use aria-invalid and aria-describedby for screen readers
  5. Show validation state - Indicate loading, success, and error states clearly
  6. Preserve user input - Don’t make users retype everything after an error