how to test for legit email in java

Solutions on MaxInterview for how to test for legit email in java by the best coders in the world

showing results for - "how to test for legit email in java"
Beatrice
11 Mar 2018
1// Java program to check if an email address 
2// is valid using Regex. 
3import java.util.regex.Matcher; 
4import java.util.regex.Pattern; 
5  
6class Test 
7{ 
8    public static boolean isValid(String email) 
9    { 
10        String emailRegex = "^[a-zA-Z0-9_+&*-]+(?:\\."+ 
11                            "[a-zA-Z0-9_+&*-]+)*@" + 
12                            "(?:[a-zA-Z0-9-]+\\.)+[a-z" + 
13                            "A-Z]{2,7}$"; 
14                              
15        Pattern pat = Pattern.compile(emailRegex); 
16        if (email == null) 
17            return false; 
18        return pat.matcher(email).matches(); 
19    } 
20  
21    /* driver function to check */
22    public static void main(String[] args) 
23    { 
24        String email = "contribute@geeksforgeeks.org"; 
25        if (isValid(email)) 
26            System.out.print("Yes"); 
27        else
28            System.out.print("No"); 
29    } 
30}