Code for validating email in java

Note that Internet Address.validate() considers [email protected][10.9.8.7] and [email protected] as valid email addresses - which they are according to the RFC. But I just tried some code and the following five strings all pass as valid e-mail addresses if you pass them to the Internet Address constructor, and "clearly", they are not valid. Though, depending on the use case (web form), you might want to treat them as invalid. Here we go: After actually trying to build my project, it seems apache commons doesn't work with Android very well, hundreds of warnings and some errors, it didn't even compile.

Restrictions in above regex are:1) A-Z characters allowed 2) a-z characters allowed 3) 0-9 numbers allowed 4) Additionally email may contain only dot(.), dash(-) and underscore(_) 5) Rest all characters are not allowed Let’s test some email addresses against above regex.Should I just do In practical terms, I doubt that a real user will enter enough invalid addresses to smash the stack limits before getting bored and aborting the program, but Java doesn't offer tail-call optimization, so you are always in danger with those sorts of algos. // Long loop here to get valid user registration info. List emails = new Array List(); emails.add("[email protected]"); emails.add("[email protected]"); emails.add("[email protected]"); emails.add("user? : false [email protected]: false Now lets modify the regex such that domain name must include at least one dot, and that the part of the domain name after the last dot can only consist of letters. The top-level domain (in these examples) must consist of two to six letters only. [email protected]"); emails.add("user'[email protected]"); //Invalid emails emails.add("@yahoo.com"); String regex = "^[a-z A-Z0-9_! `~^.-] @[a-z A-Z0-9.-] $"; Pattern pattern = Pattern.compile(regex); for(String email : emails) Output: [email protected]: true [email protected] : true [email protected]: true user? 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]"); //Invalid emails emails.add("[email protected]"); emails.add("[email protected]"); emails.add("[email protected]"); emails.add("[email protected]"); emails.add("[email protected]"); String regex = "^[\w! [email protected] – “.a” is not a valid tld, last tld must contains at least two characters 4. mkyong()*@– email’s is only allow character, digit, underscore and dash 8.

You must have an account to comment. Please register or login here!