Skip to content
Advertisement

Validating not required form fields with Yup?

I want to validate a not required form field with Yup

JavaScript

lastname: Yup.string.nullable().notRequired(). I don’t how to proceed further because I have multiple condition to validate the field if the input was given.

My validation condition are:

  1. Should contain only alphabets.
  2. Should be atleast minimum 2 and maximum 20 alphabets.

Advertisement

Answer

You should use similar match pattern you already have for firstname. One possible way of doing is like this:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement