Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,870 questions

51,793 answers

573 users

How to check if email address is valid in Java

1 Answer

0 votes
import java.util.regex.Matcher; 
import java.util.regex.Pattern; 

public class MyClass {
    public static boolean EmailValid(String email) { 
        String regex = "^[a-zA-Z0-9_+&*-]+(?:\\."+ 
                        "[a-zA-Z0-9_+&*-]+)*@" + 
                        "(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$"; 
                              
        if (email == null) 
            return false; 
            
        Pattern email_pat = Pattern.compile(regex); 
        
        return email_pat.matcher(email).matches(); 
    } 
    public static void main(String args[]) {
        String email = "a@email.com"; 
      
        System.out.println((EmailValid(email) == true) ? "yes" : "no");
      
        email = "a@email"; 
      
        System.out.println((EmailValid(email) == true) ? "yes" : "no");
      
        email = "a@email.co.uk"; 
      
        System.out.println((EmailValid(email) == true) ? "yes" : "no");
    }
}



/*
run:

yes
no
yes

*/

 



answered Sep 24, 2019 by avibootz
edited Sep 24, 2019 by avibootz

Related questions

2 answers 212 views
1 answer 177 views
1 answer 177 views
1 answer 183 views
1 answer 233 views
1 answer 56 views
...