28

for example, While entering an email in the TextInput, it should validate and display the error message. where the entered email is valid or not

enter image description here

2
  • 1
    What are you using to display the error messages and exclamation marks when input is invalid? Thx in advance Commented Sep 14, 2016 at 21:07
  • Yeah what is the black popup prompt you are using? Commented Jul 27, 2017 at 14:10

2 Answers 2

60

You can use a regex to check if the mail entered is valid.

Regex function

validateEmail = (email) => {
  var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(email);
};

Submit text input function

onSubmit = () => {
if (!this.validateEmail(this.state.text_input_email)) {
  // not a valid email
} else {
  // valid email
}
Sign up to request clarification or add additional context in comments.

2 Comments

This rejex does not support : email@[123.123.123.123]
I need to validate without submitting the form. How can I achieve that?
2

You can validate your input value using onBlur event on TextInput You can apply your regex or check conditions on this event.

Like this:

<TextInput
      onBlur= () => {
        //Conditions or Regex
      }
/>

In your case, Regex function:

validateEmail = (email) => {
var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(email);
};

Text Input Code:

<TextInput
  onBlur= () => {
    if (!this.validateEmail(this.state.text_input_email)) {
       // not a valid email
    } else {
       // valid email
    }
  }
/>

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.