perscrew / react-native-form-validator

React native library to validate form fields
127 stars 55 forks source link

Confirm Password check #41

Open zapcriativo opened 4 years ago

zapcriativo commented 4 years ago

Hello,

I have 2 fields, password and confirm password. I would like to know how to perform this validation, to compare the value of confirm password with that of the password field?

arochedy commented 4 years ago

Hello,

I just dit something like this.

I just publish a a PR : https://github.com/perscrew/react-native-form-validator/pull/45

I can explain you how I did :

I have 2 fields : newPaswword & confirmPassword with this rules :

    var result = this.validate({
      newPassword: { required: true, minlength: 8, equalPassword: this.state.confirmPass },
      confirmPass: { required: true, minlength: 8, equalPassword: this.state.newPassword }
    });

I create the equalPassword validation rule.

in defaultRules.js I add the function :

  equalPassword(dataToCompare, value) {
   return dataToCompare == value;
  }

And I had in defaultMessages.js :

   en: {
      numbers: 'Must be a valid number.',
      email: 'Must be a valid email address.',
      required: 'This is mandatory.',
      date: 'Must be a valid date ({1}).',
      minlength: 'Length must be greater than {1}.',
      maxlength: 'Length must be lower than {1}.',
      equalPassword : 'Passwords are differents'
    },
    // French language
    fr: {
      numbers: 'Ce champ doit être un nombre valide.',
      email: 'Ce champ doit être une adresse email valide.',
      required: 'Ce champ est obligatoire.',
      date: 'Ce champ doit correspondre à une date valide ({1}).',
      minlength: 'Le nombre de caractère doit être supérieur à {1}.',
      maxlength: 'Le nombre de caractère doit être inférieur à {1}.',
      equalPassword : 'Les mots de passes sont differents'
    },
apatil-deqode commented 3 years ago

Hello,

I have seen one more issue, is there any solution for this? Please review below code:

return validate({
      password: {required: true, minlength: 8, maxlength: 12},
      confirmPassword: {required: true, equalPassword: password},
    });
In case of empty confirmPassword I'm getting:
**The confirmPassword is mandatory.**

But I want to break down confirmPassword
ex:     **The confirm password is mandatory.**
perscrew commented 3 years ago

Can you try to remove the required:trueon confirmPassword ?

apatil-deqode commented 3 years ago

@perscrew , Thanks but, If I'll remove it, It is accepting an empty field as well.

And I have fixed it by using labels

const {validate, isFieldInError, getErrorsInField} = useValidation({
    state: {password, confirmPassword},
    messages: ValidationMessages,
    labels: ValidationLables,
  });

  export const ValidationLables = {
  confirmPassword: 'confirm password',
};