Built-In Validation Rules
You'll learn: How to add field validation directly in React Hook Form's register options without writing custom logic.
The Declarative Validation Approach
Instead of manually writing validation functions, React Hook Form lets you declare validation rules as configuration options passed to register. Think of it like setting HTML5 form attributes (required, minLength, etc.), but with better error handling and control.
The register function accepts a second argument—an options object where you specify validation rules. These rules run automatically when the form is submitted or the field is touched.
Common Built-In Rules
<input
{...register("username", {
required: "Username is required",
minLength: {
value: 3,
message: "Must be at least 3 characters"
},
maxLength: {
value: 20,
message: "Cannot exceed 20 characters"
}
})}
/>
<input
type="email"
{...register("email", {
required: true,
pattern: {
value: /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i,
message: "Invalid email address"
}
})}
/>
<input
type="number"
{...register("age", {
min: { value: 18, message: "Must be 18+" },
max: { value: 120, message: "Invalid age" },
valueAsNumber: true // converts string to number
})}
/>
Notice you can provide just true for simple rules, or an object with value and custom message for better error feedback. The pattern rule accepts regex for complex validation like email formats.
Key Takeaway: React Hook Form's built-in validation rules let you declaratively enforce required, minLength, maxLength, pattern, min, max, and more—all without writing validation functions manually.