In this post, we will see how to validate email address in java.
There are times when you have to validate email address provided by user. You might want to write your own logic to validate email addresses but there are lots of standard regular expressions or libraries which can help you validate email address and provide great results.
Table of Contents
Email address is made up of local part, @ and followed by domains. You can go through wiki page for format of email address.
Using regular expressions
We can use regular expression provided by OWASP Validation Regex Repository which is considered to be safe and will solve the purpose in most of the cases.
Here is the regular expression
Here is complete java program for email address validation in java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
package org.arpit.java2blog; import java.util.regex.Pattern; public class EmailValidatorMain { public static boolean isValid(String email) { String emailRegex = "^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@" + //part before @ "(?:[a-zA-Z0-9-]+\\.)+[a-zA-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 email2 = "@java2blog.com"; String[] emails= {email1,email2,email3}; for (int i = 0; i < emails.length; i++) { String email=emails[i]; if (isValid(email)) System.out.print(email+" is valid email"); else System.out.print(email+" is invalid email"); System.out.println(); } } } |
When you run above program, you will get below output
Using apache common validator library
You can also use inbuilt apache librabry to validate the address. If you are using maven, then you need to add below dependency in your pom.xml.
1 2 3 4 5 6 7 8 |
<!-- https://mvnrepository.com/artifact/commons-validator/commons-validator --> <dependency> <groupId>commons-validator</groupId> <artifactId>commons-validator</artifactId> <version>1.4.0</version> </dependency> |
Here is java program for the same.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package org.arpit.java2blog; import org.apache.commons.validator.routines.EmailValidator; public class EmailValidatorMain { public static boolean isValid(String email) { // Get an EmailValidator instance first EmailValidator validator = EmailValidator.getInstance(); // check valid email address if (!validator.isValid(email)) { return false; } return true; } /* driver function to check */ public static void main(String[] args) { String email2 = "@java2blog.com"; String[] emails= {email1,email2,email3}; for (int i = 0; i < emails.length; i++) { String email=emails[i]; if (isValid(email)) System.out.print(email+" is valid email"); else System.out.print(email+" is invalid email"); System.out.println(); } } } |
When you run above program, you will get below output
That’s all about validate email address in java.