Check if Email Address is Valid or not in Java Last Updated : 28 Nov, 2024 Comments Improve Suggest changes Like Article Like Report Validating email addresses means they meet the correct format by avoiding invalid inputs. To Validate email addresses in Java, we can use Regular Expressions (Regex).Example:The below example example uses Pattern and Matcher from the java.util.regex package to validate email addresses. Java // Java program to check if an email address // is valid using Regex import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.ArrayList; public class EmailValidate { // Method to check if the email is valid public static boolean isValid(String email) { // Regular expression to match valid email formats String emailRegex = "^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@" + "(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$"; // Compile the regex Pattern p = Pattern.compile(emailRegex); // Check if email matches the pattern return email != null && p.matcher(email).matches(); } public static void main(String[] args) { // List of email addresses to validate ArrayList<String> e = new ArrayList<>(); e.add("[email protected]"); e.add("writing.geeksforgeeks.org"); // Loop through emails and validate each for (String email : e) { System.out.println(email + ": " + (isValid(email) ? "Yes" : "No")); } } } Output[email protected]: Yes writing.geeksforgeeks.org: No Explanation: In the above example, the "emailRegex" checks for valid email formats like [email protected]. The Pattern class compiles the regex and matches it against the email string. Valid emails return "Yes" and the invalid emails return "No". Comment More infoAdvertise with us Next Article Check if Email Address is Valid or not in Java P pranav gupta Improve Article Tags : Misc Java Java Programs java-regular-expression Practice Tags : JavaMisc Similar Reads Java program to read all Emails present in a Given file Given a file as input.txt containing some email ids which are mixed with other data. The task is to read this input file line by line and if any email id is found in that line, write that email id to another file, which is output.txt. Example: Input: input.txt Output: output.txt Approach: To detect 3 min read How to Extract Domain Name From Email Address using Java? Given some Strings as Email addresses, the task is to extract the domain name from it Examples: Input: test_str = â[email protected]â Output: geeksforgeeks.org Explanation: Domain name, geeksforgeeks.org extracted. Input: test_str = â[email protected]â Output: gmail.com Explanation: Domain name, g 3 min read How to Check if a String Contains only Digits in Java? In Java, to check if a string contains only digits, we can use various methods such as simple iteration, regular expressions, streams, etc.Example:The example below uses the Character.isDigit() method to check each character of the string to ensure all characters are digits. This is the most simple 3 min read Java Program to Check Whether the String Consists of Special Characters In Java, special characters refer to symbols other than letters and digits, such as @, #, !, etc. To check whether the String consists of special characters, there are multiple ways, including using the Character class, regular expressions, or simple string checks.Example:In this example, we will us 4 min read How to validate a Password using Regular Expressions in Java Given a password, the task is to validate the password with the help of Regular Expression. A password is considered valid if all the following constraints are satisfied: It contains at least 8 characters and at most 20 characters.It contains at least one digit.It contains at least one upper case al 3 min read Like