Search code examples
reactjsredux-form

validate confirmation password in redux form


How to validate password confirmation in react form?

const validate = values => {
  const errors = {};
  if (!values.username) {
    errors.username = 'Required';
  }
  if (!values.password) {
    errors.password = 'Required';
  }
  if (!values.confirmpassword ) {
    errors.confirmpassword = 'Required' ;
  }

   return errors;


};

export default validate;

Here is my validation page I tried confirm password validation during mismatches occurs... That doesn't works.. Is there anyone willing to help me??


Solution

  • Try the following logic.

    const validate = values => {
      const errors = {};
      if (!values.username) {
        errors.username = 'Required';
      }
      if (!values.password) {
        errors.password = 'Required';
      }
      if (!values.confirmpassword ) {
        errors.confirmpassword = 'Required' ;
      } else if (values.confirmpassword !== values.password) {
        errors.confirmpassword = 'Password mismatched' ;
      }
    
       return errors;
    
    
    };
    
    export default validate;