Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set match password in react Js

I need some help how could I match the password in react js. I used ant design the first password is working but for conform password I put statement its not working how could I do it

handlePasswordChange = event => {
  this.setState({
    password: event.target.value,
  });
};
handleConfirmPassword = event => {
  if (event.handleConfirmPassword !== event.handlePasswordChange) {
    message.error('error');
  }
};

these are fun and below is the ant design

<FormItem {...styles.formItemLayout} label="Password">
  {getFieldDecorator('Password', {
    rules: [{ required: true, message: 'Password is Required!' }],
  })(
    <Input
      onChange={this.handlePasswordChange}
      name="password"
      type="password"
      value={password}
      style={styles.margin}
    />,
  )}
</FormItem>
<FormItem {...styles.formItemLayout} label="Confirm Password">
  {getFieldDecorator('Confirm Password', {
    rules: [{ required: true, message: 'Confirm your Password!' }],
  })(
    <Input
      name="password"
      type="password"
      style={styles.margin}
      onChange={this.handleConfirmPassword}
    />,
  )}
</FormItem>
like image 464
ALVIN Avatar asked Jul 02 '18 21:07

ALVIN


2 Answers

Assuming that both your password and confirmPassword are in state.

this.state = {
    password: '',
    confirmPassword: ''
}

handleSubmit = () => {
    const { password, confirmPassword } = this.state;
    // perform all neccassary validations
    if (password !== confirmPassword) {
        alert("Passwords don't match");
    } else {
        // make API call
    }
}
like image 196
Erick Avatar answered Oct 24 '22 03:10

Erick


Try this:

handleConfirmPassword = (event) => {
    if (event.target.value !== this.state.password) {
      message.error('error');
    }
}

You can also set your state like:

state = {
    password: '',
    confirmPassword: ''
}

And then, you can check the match on the handleConfirmPassword and on the submit.

handleConfirmPassword = (event) => {
    if (event.target.value !== this.state.password) {
      message.error('error');
      this.setState({confirmPassword: event.target.value})
    }
}

And then, a submit handler to the form:

handleSubmit = (event) => {
   if(this.state.password !== this.state.confirmPassword){
       message.error("The passwords doesn't match")
       return false; // The form won't submit
   }
   else return true; // The form will submit
}
like image 9
reisdev Avatar answered Oct 24 '22 03:10

reisdev