R
R
Ruslan Absalyamov2018-03-06 21:54:32
React
Ruslan Absalyamov, 2018-03-06 21:54:32

How to display an error when confirming a password?

I can not implement password confirmation during registration. To write an error, below the password confirmation field, a phrase like the password does not match
Implementation example

import React, { Component } from 'react';
import Input, { InputLabel, InputAdornment } from 'material-ui/Input';
import { FormControl, FormHelperText } from 'material-ui/Form';
import TextField from 'material-ui/TextField';
import { Link } from 'react-router-dom';
import Button from 'material-ui/Button';

export default class Registration extends Component{
    constructor(props){
        super(props);
        this.state = {
            login: '',
            password: '',
            confirmPassword: '',
            email: '',
            errors: ''
        };
        this.handleSubmit = this.handleSubmit.bind(this);
        this.handleChange = this.handleChange.bind(this);
    }
...

    handleSubmit(e){
        e.preventDefault();
        if (this.state.password === this.state.confirmPassword){
            console.log(true);
        } else {
            console.log(false);
            this.state.errors = 'Пароль не совпадает'
        }
        console.log(this.state);
    }
    render(){
        return(
            <div className='registration'>
                <h1>Регистарация</h1>
                <form onSubmit={this.handleSubmit}>
                    ...
                    {this.state.errors ? this.state.errors : ''}
                    ...
                </form>
            </div>
        )
    }
}

All code https://codesandbox.io/s/03r78ljrj0

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Ruslan Absalyamov, 2018-03-06
@rusline18

Instead of this.state.errors = "Password does not match"; - this.setState({ errors: 'Password doesn't match' })

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question