0

I do have a TextField component to take email as input.

<div className="form-group" style={styles.popupContainer}>
  <label>
    <IntlMessages id="DIC_SETTINGS_CLIENT_PIC_EMAIL" />
  </label>
  <label className="text-danger" style={styles.star_style}>
    <sup>{"*"}</sup>
  </label>
  <TextField
    error={errorInputs.email}
    margin="normal"
    fullWidth
    id={"email"}
    value={item.email}
    onChange={this.handleEmailChange("email")}
    onBlur={() => this.handleBlur("email")}
  />
</div>;

onChange()

handleEmailChange = email => event => {
        let changedEmailValue = event.target.value;

        switch (email) {
            case "email":
                let temp = this.state.item;
                temp.email = changedEmailValue;
                this.setState({
                    item: temp
                });
                break;
        }
    };

How to write code for validation of email in this context

0

1 Answer 1

1

You can easily validate your email by testing event.target.value with a Regex expression. Here is a sample implementation for your code.

Here is a live sandbox. Test by clicking on the console tab and entering a valid email on the input field.

Edit flamboyant-bohr-je64k

handleEmailChange = (event) => {
  let changedEmailValue = event.target.value;
  const isValidEmail = this.validateEmail(changedEmailValue);
  if (isValidEmail) {
    this.setState({
      item: event.target.value,
    });
  } else {
    console.log("It's not a valid email");
  }
};

validateEmail = (email) => {
  if (/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.test(email)) {
    return true;
  } else {
    return false;
  }
};

 <TextField
  error={errorInputs.email}
  margin="normal"
  fullWidth
  id={"email"}
  value={item.email}
  onChange={(e) => {
    this.handleEmailChange(e);
  }}
  onBlur={() => this.handleBlur("email")}
/>;

Update: Implemented with Switch

handleEmailChange = (event) => {
  let changedEmailValue = event.target.value;
  const isValidEmail = this.validateEmail(changedEmailValue);

  switch (event.target.id) {
    case "email":
     isValidEmail
          ? this.setState({ item: event.target.value })
          : this.setState({ item: "" });
      break;
    default:
      break;
  }
};

validateEmail = (email) => {
  if (/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.test(email)) {
    return true;
  } else {
    return false;
  }
};

 <TextField
  error={errorInputs.email}
  margin="normal"
  fullWidth
  id={"email"}
  value={item.email}
  onChange={(e) => {
    this.handleEmailChange(e);
  }}
  onBlur={() => this.handleBlur("email")}
/>;

Sign up to request clarification or add additional context in comments.

2 Comments

How to implement the same inside Switch
I have updated the answer with Switch. Do check it out.

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.