K
K
KononovD2019-11-04 15:36:34
React
KononovD, 2019-11-04 15:36:34

How to make it so that when submitting the form, an error message is written next to each incorrectly filled input?

Hello, here is an example of a form:
5dc019e29c6ba682854834.png
let's say we just click on the button. Under both inputs, an inscription should appear stating that these fields are required. if some field is incorrectly filled in - the corresponding message:
5dc01acd2e7d4310576962.png
Tell me how to do it? Thank you.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
Nadim Zakirov, 2019-11-04
@KononovD

The pattern attribute will help you , you can write a regular expression in it to validate the field, and also specify a message that should appear if the field is filled incorrectly (there are several ways).
Ready list of regular expressions see here.

A
Alexander Dozmorov, 2019-11-04
@Dozalex

You can look towards specialized libraries.
For example https://github.com/jquence/yup
Integrates well with form libraries like Formik and react-final-form

C
camelCaseVlad, 2019-11-04
@camelCaseVlad

To create forms in React projects, use Formic.
https://jaredpalmer.com/formik/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question