3

I had a condition if the two textfield are the same it will proceed, if not then the button is disabled. I know I'm doing this wrong.

onSubmit() {
        debugger
        if  (this.state.password == this.state.password2)
            alert('same');
        else 
            alert('error');
            this.disable;
}
1
  • Pro tips for posting: (1) please do not offer voting advice. If people wish to vote on your posts, they will, and asking for people not to downvote will probably have the opposite effect; (2) try to refrain from "please help me" and other forms of begging and pleading - it's another good way to get downvotes. Experienced users find it annoying, since hundreds of people beg every single day. (3) When referring to yourself, please use a capital letter "I" - all-lower-case posting might be OK on Facebook, but readability is important here. Thanks! Commented Aug 21, 2017 at 17:52

3 Answers 3

4

First, dont check onSubmit but on change (you dont want to disable it AFTER it was submitted) for both your input. Add a state value for your disabledButton, and set it to false only if both input are the same (in the onChange event).

You can set disabled button inside your JSX like this

<button disabled={this.state.disabledButton} />
Sign up to request clarification or add additional context in comments.

Comments

1

You can do that in your render function:

render() {
  const submitDisabled = this.state.password !== this.state.password2

  return (
    // ...
      <button disabled={submitDisabled}>Submit</button>
    // ...
  )
}

4 Comments

thank you for answering but im using <CustomDialog title="Change Password" onSubmit={this.onSubmit.bind(this)}>
What is the CustomDialog? Does it provide any way to change the state of the submit button?
oh it;'s just a modal
I might need to see its API
0

This will work Perfect. Try this

render() {
  const submitDisabled = password.length > 0
  return (
    // ...
      <button disabled={submitDisabled}>Submit</button>
    // ...
  )
}

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.