2

I have a condition in email validation.

  1. If the email textbox is empty.. Return true

  2. If the email textbox is not empty, check the text box is that valid email or not

  3. If it was not valid email.. Return false

     isEmailValid = () => {
             const { Email} = this.state;
    
     var filter = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/;
    
             if(Email=='') {        
                 return true;        
             }
    
             else{                
                 Alert.alert("Error", I18n.strings("requiredField", { name: I18n.strings("account.emailId") }));
                 return false;        
             }
    
         return true;
     }
    

What is condition to do in else?

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Allen Bert
  • 158
  • 2
  • 5
  • 20

1 Answers1

6

You can try:

isEmailValid = () => {
     let email = this.state.email
     let pattern = /^(([^<>()\[\]\\.,;:\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 pattern.test(String(email).toLowerCase())
}
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Steve
  • 914
  • 8
  • 17