Specific language Yup validation

Viewed 2206

I've been using Yup and Formik for form validation in React. How can I set validation for input language? I want to a user can (and only can) write in a specific language (change the default language of the keyboard)

const validationSchema = yup.object().shape({
  first_name: yup.string().required("Required *"),
  last_name: yup.string().required("Required *"),
 });
1 Answers

You can use Regex pattern. For example for the English language:

const nameRegex = /^[A-Za-z]+$/;

const validationSchema = yup.object().shape({
  first_name: yup.string().matches(nameRegex, "Only English letters").required("Required *"),
  last_name: yup.string().matches(nameRegex, "Only English letters").required("Required *"),
 });
Related