question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Matching fields example (ie. Passwords)

See original GitHub issue

I’m struggling with getting a Yup validation working to ensure two fields match (in this case, password and password confirm).

    validationSchema: Yup.object().shape({
      password: Yup.string()
        .required('Password is required'),
      passwordConfirm: Yup.mixed().test('match', 'Passwords do not match', function (password) {
        return password === this.parent.passwordConfirm
      }).required('Password confirm is required')
    }),

I’m new to Formik & Yup (and relatively new to JS generally). Really appreciate any pointers.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:7
  • Comments:27 (2 by maintainers)

github_iconTop GitHub Comments

332reactions
jquensecommented, Jan 2, 2018

FYI folks this has gotten easier in the most recent version of yup. oneOf and notOneOf` now support refs

validationSchema: Yup.object({
  password: Yup.string().required('Password is required'),
  passwordConfirm: Yup.string()
     .oneOf([Yup.ref('password'), null])
     .required('Password confirm is required')
})
``
123reactions
eonwhitecommented, Jul 25, 2017

Yup doesn’t make this common usecase as easy as it should. Here’s how I’ve gotten this to work (a slight modification of code in https://github.com/jquense/yup/issues/97)

function equalTo(ref: any, msg: any) {
  return Yup.mixed().test({
    name: 'equalTo',
    exclusive: false,
    message: msg || '${path} must be the same as ${reference}',
    params: {
      reference: ref.path,
    },
    test: function(value: any) {
      return value === this.resolve(ref);
    },
  });
}
Yup.addMethod(Yup.string, 'equalTo', equalTo);

then in your validationSchema:

    passwordConfirm: Yup.string().equalTo(Yup.ref('password'), 'Passwords must match').required('Required'),
Read more comments on GitHub >

github_iconTop Results From Across the Web

Password Matching using JavaScript - GeeksforGeeks
It is the simple method to verify the password matches. First password is stored into a password1 variable and confirm password is stored...
Read more >
Confirm password validation in JavaScript - javatpoint
To create a valid password, both the password and confirm password fields value must be matched. First one, we will check for a...
Read more >
SSO (Saved Passwords) Apps: Custom Formulas
Example : Password for all users is apple. Type in: apple. Fields, Insert the whole contents of a field in Clever. Example: Password...
Read more >
Test Cases for Password and Forgot Password Functionality
Presenting test cases for password and forgot password fields. These test cases will help you in your interviews as well as in the...
Read more >
Remember passwords and fill out web forms for Internet ...
Learn how to fill in forms on websites and remember online passwords automatically with AutoComplete in Internet Explorer.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found