Implementing validation
Including validation on a form improves the user experience as you can provide immediate feedback on whether the information that's been entered is valid. In this section, we are going to add validation rules to the forms for asking and answering questions. These validation rules will include checks to ensure a field is populated and that it contains a certain number of characters.
Implementing validation on the ask form
We are going to implement validation on the ask form by following these steps:
- In
AskPage.tsx
, we are going to make sure that the title and content fields are populated by the user with a minimum number of characters. First, import theFieldError
styled component:import { ... FieldError, } from './Styles';
- Destructure the form error messages from the
useForm
hook:const { register, errors } = useForm<FormData>();
- Next, configure the form so that it invokes the validation rules...