How do i prevent formik yup ( regex ) validation from disappearing

Viewed 53

This is my regex

        const firstNameSpecialChars = /[^\/\\#+()$~%.":;*<>{}@]/;

This is my validation

     const validationSchema = Yup.object().shape({
    email: Yup.string()
      .email(ENTER_VALID_EMAIL_MESSAGE)
      .required(EMAIL_IS_REQUIRED_MESSAGE),
    firstname: Yup.string()
      .trim()
      .matches(
        firstNameSpecialChars,
        'These characters are not allowed in name fields',
      ),

I want an error to be shown when there is a regex match. This works, but there is a problem... As soon as i type after the hash symbol the error disappears but when there is a symbol the error shows up

enter image description here

This is how my error looks like when I type regex chars

enter image description here

which is not what i want if there is special char i want the error to be there till the user removes the special char


1 Answers

Found a solution :

I had to do this :

Regex

/^[^#+()$~%.":;*<>{}@\\/\\]+$/

This gives me the error when anyone of the stuff inside the [ ] matches

Related