0

I am new to react and created a login form with a few set of validations to raise errors in case of blank username or password. On the very first instance, when nothing is inserted and the Submit is clicked, it works fine. But once the value is changed with a value inserted by the user and then deleting from the text input, the variable gets a value of "" and never gets back to null value. This avoids my special treatment where I inserted !this.state.username. It gets stuck at that moment and the form submission goes anyway to the server. I have tried trim as well but it didn't work

class LoginClassComponent extends React.Component {


constructor(props) {
    super(props);
    this.state = {
      username: this.props.username,
      password: this.props.password,
      errorUsername: null,
      errorPassword: null,
    };

this.handleValidation = this.handleValidation.bind(this);
this.handleChange = this.handleChange.bind(this);

}

//assign textbox values to props
  handleChange = (e) => {
    this.setState({
      [e.target.name]: [e.target.value],
    });
  };
  //handle input validation
  handleValidation = (event) => {
    if (!this.state.username) {
      this.setState({ errorUsername: "Please enter User Name" });
      event.preventDefault();
    }

    if (!this.state.password) {
      this.setState({ errorPassword: "Please enter Password" });
      event.preventDefault();
    }

2 Answers 2

1

Your handleChange is incorrect, you should remove the brackets around value:

handleChange = (e) => {
    this.setState({
      [e.target.name]: e.target.value
    });
  };
Sign up to request clarification or add additional context in comments.

2 Comments

sorry, it didnt work. I applied below code < handleValidation = (event) => { console.log("USername: ", this.state.username) if (!this.state.username || this.state.username.length === 0) { //tried with 1 and 0 this.setState({ errorUsername: "Please enter User Name" }); event.preventDefault(); } > but when I am printing username it is still as below USername: [""]0: ""length: 1 proto: Array(0)
I see, there is an error in your handleChange, I have updated my answer. You can also remove the second test once you make this change
0

for this scenario I would go with function components and hooks, but for the sake of simplicity and not knowing what the rest of your implementation looks like, I'd go as follows. Assuming you are triggering handleValidation either on field blur or on submit click, try modifying it like so:

handleValidation = (event) => {
    event.preventDefault();
    let errorUsername = null;
    let errorPassword = null;
    if (event.target.name === 'username') {
        errorUsername = (event.target.value === '') ? "Please enter User Name" : null;

    } else if (event.target.name === 'password') {
        errorPassword = (event.target.value === '') ? "Please enter Password" : null;

    }
    this.setState({ errorUsername, errorPassword });
}

Let me know if you need any further assistance.

Cheers! 🍻

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.