Skip to content
Advertisement

Formik, Yup Password Strength Validation with React

I am fairly new to React, and i have a sign up page where i have a password field to be validated with a Regex.

I am using Formik and Yup for validations, but i have encountered an error where it says the property where the length function is being called is undefined when i type in the “password” field.

There is a function called “matches” in yup, which i am trying to utilize for checking a regex. That is when i get this error. I removed this validation, after which other validations set to password field worked fine.

Below is the code for the SignUp.js file :-

JavaScript

Here is the error in chrome console :-

JavaScript

Advertisement

Answer

You need to pass an actual RegExp object to matches, not a string. Just replace the double quotes with forward slashes in your password schema:

EDIT: Updated to use regex from @Bren

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