Java validating email address

The source code is compiled and tested in my dev environment.

Just use Java Mail's built-in utility, and move on to more meaningful things.

^ #start of the line [_A-Za-z0-9-\ ] # must start with string in the bracket [ ], must contains one or more ( ) ( # start of group #1 \.[_A-Za-z0-9-] # follow by a dot "." and string in the bracket [ ], must contains one or more ( ) )* # end of group #1, this group is optional (*) @ # must contains a "@" symbol [A-Za-z0-9-] # follow by string in the bracket [ ], must contains one or more ( ) ( # start of group #2 - first level TLD checking \.[A-Za-z0-9] # follow by a dot "." and string in the bracket [ ], must contains one or more ( ) )* # end of group #2, this group is optional (*) ( # start of group #3 - second level TLD checking \.[A-Za-z] # follow by a dot "." and string in the bracket [ ], with minimum length of 2 ) # end of group #3 $ #end of the line The combination means, email address must start with “_A-Za-z0-9-\ ” , optional follow by “.[_A-Za-z0-9-]”, and end with a “@” symbol. Email is valid : [email protected], true Email is valid : [email protected], true Email is valid : [email protected], true Email is valid : [email protected], true Email is valid : [email protected], true Email is valid : [email protected] , true Email is valid : [email protected], true Email is valid : [email protected] , true Email is valid : mkyong [email protected], true Email is valid : [email protected], true Email is valid : mkyong , false Email is valid : [email protected] , false Email is valid : [email protected] , false Email is valid : [email protected], false Email is valid : [email protected] , false Email is valid : [email protected], false Email is valid : mkyong()*@, false Email is valid : [email protected]%*, false Email is valid : [email protected], false Email is valid : [email protected], false Email is valid : [email protected]@, false Email is valid : [email protected] , false PASSED: Valid Email Test([

The email’s domain name must start with “A-Za-z0-9-“, follow by first level Tld (.com, .net) “.[A-Za-z0-9]” and optional follow by a second level Tld (au, my) “\.[A-Za-z]”, where second level Tld must start with a dot “.” and length must equal or more than 2 characters.

List emails = new Array List(); emails.add("[email protected]"); emails.add("[email protected]"); emails.add("[email protected]"); emails.add("[email protected]"); emails.add("[email protected]"); emails.add("[email protected]"); emails.add("[email protected]"); //Invalid emails emails.add("@yahoo.com"); String regex = "^[A-Za-z0-9 _.-] @(.

)$"; Pattern pattern = Pattern.compile(regex); for(String email : emails) Output: [email protected]: true [email protected] : true [email protected]: true [email protected]: true [email protected] : true [email protected] : true [email protected] : true @: false This regex example uses all the characters permitted by RFC 5322, which governs the email message format. :[a-z A-Z0-9-] \.) [a-z A-Z]$"; Pattern pattern = Pattern.compile(regex); for(String email : emails) Output: [email protected]: true [email protected] : true [email protected]: true [email protected]: true [email protected]: true [email protected]: false [email protected]

Search for java validating email address:

java validating email address-23

Leave a Reply

Your email address will not be published. Required fields are marked *

One thought on “java validating email address”