1

so it's not kind of a problem, but when I'm trying to validate my form I usually use a large amount of

if(){
}else if(){
}
.
.
.
else{
}

but it's both time-consuming and in the means of clean code, it doesn't really appear good. So is there kind of a Form validation component or a better way to do this? any reference would be super useful.

Ahmet Emre Kilinc
  • 5,489
  • 12
  • 30
  • 42
ali mosavian
  • 85
  • 1
  • 9

2 Answers2

2

you can use Formik with Yup

validation schema


 const SignupSchema = Yup.object().shape({
   firstName: Yup.string()
     .min(2, 'Too Short!')
     .max(50, 'Too Long!')
     .required('Required'),
   lastName: Yup.string()
     .min(2, 'Too Short!')
     .max(50, 'Too Long!')
     .required('Required'),
   email: Yup.string().email('Invalid email').required('Required'),
 });

nice get started here

Yoel
  • 7,555
  • 6
  • 27
  • 59
1
  1. If you prefer to use a package like formik, You can use the answer given by @Yoel.
  2. You have Default HTML5 validation that you can use and set validation messages using JS setCustomValidity() API.
janmejay
  • 44
  • 3