0

I'm new to react, my objective is to show border in red color when we click on send button without entering any value in input text field. I don't know how to get border red in material-ui. Do i need to validate the input field? If yes then do i need to validate with letters and alphanumeric regex? I'm bit confused in here.

Here is the code:

 <TextField
            margin="dense"
            fullWidth
            variant="outlined"
            placeholder="Enter a Message"
            name="msg"
            id="text"
          />
          <Button onClick={this.handleSubmit}>Send</Button>

Can anyone assist me in this query?

1 Answer 1

3

Add a value property in TextField and after submitting check value property is empty or not if empty then add a dynamic class in TextField.

state = { msg: "" }

Add an onChange and classname inside TextField attribute.

onChange = e => this.setState({ msg: e.target.value })
classname={this.state.msg !== "" ? "borderRed":""}
Sign up to request clarification or add additional context in comments.

2 Comments

hi, after adding value property i couldn't enter any value
Let me know if it helps or not.

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.