Check if email address valid or not in Java
Given a string, find if the given string is a valid email or not.
Input : email = "contribute@geeksforgeeks.org" Output : Yes Input : email = "contribute@geeksforgeeks..org" Output : No There is an extra dot before org.
Prerequisite : Regular Expressions in Java
We use below regular expression provided in OWASP Validation Regex repository.
^[a-zA-Z0-9_+&*-] + (?:\\.[a-zA-Z0-9_+&*-]
+ )*@(?:[a-zA-Z0-9-]+\\.) + [a-zA-Z]{2, 7}
// Java program to check if an email address // is valid using Regex. import java.util.regex.Matcher; import java.util.regex.Pattern; class Test { public static boolean isValid(String email) { String emailRegex = "^[a-zA-Z0-9_+&*-]+(?:\\."+ "[a-zA-Z0-9_+&*-]+)*@" + "(?:[a-zA-Z0-9-]+\\.)+[a-z" + "A-Z]{2,7}$"; Pattern pat = Pattern.compile(emailRegex); if (email == null) return false; return pat.matcher(email).matches(); } /* driver function to check */ public static void main(String[] args) { String email = "contribute@geeksforgeeks.org"; if (isValid(email)) System.out.print("Yes"); else System.out.print("No"); } } |
chevron_right
filter_none
Output:
Yes
This article is contributed by Pranav. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Check if URL is valid or not in Java
- Check if a given string is a valid number (Integer or Floating Point) in Java
- Program to check valid mobile number
- Send email using Java Program
- Sending email through Java with SSL / TLS authentication
- Pinging an IP address in Java | Set 1
- Finding IP address of a URL in Java
- Pinging an IP address in Java | Set 2 (By creating sub-process)
- Java program to find IP address of your computer
- Valid variants of main() in Java
- Check for string rotation in Java
- How to check if a key exists in a HashMap in Java
- Check if a File is hidden in Java
- Check if a value is present in an Array in Java
- Java program to check whether a string is a Palindrome



