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>
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
}
}
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With