Email Address Validation

I have a the following code to validate Email addresses, it works, but I'm not sure if it covers all situations :

staticboolean String_Is_An_Email_Address(String Input)

{

if (Input==null)returnfalse;

try

{

new InternetAddress(Input).validate();

Pattern p=Pattern.compile("^[\\w-\\.]+@([\\w-]+\\.)+[\\w-]{2,4}$");// Set the email pattern string

Matcher m=p.matcher(Input);// Match the given string with the pattern

return m.matches();// Check whether match is found

}

catch (Exception e){returnfalse;}

}

Is there anything wrong with it ?

Frank

[1316 byte] By [Ni_Mina] at [2007-11-27 6:23:04]
# 1
The reason I'm asking is that when I feed it with .@123.com it returns true, but that's not a valid address, or is it ?Frank
Ni_Mina at 2007-7-12 17:40:36 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2
So, it seems that you already know the answer to your question. :-)No, your code is not complete. Full syntactic validation of email addressesis a large task. And even if done perfectly, it doesn't tell you that the addressis valid.
bshannona at 2007-7-12 17:40:36 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3

I've searched the web for the pattern, but none for java so far. I have a huge text file with valid and invalid email addresses, I want to clean it up with a pattern match, but I can't find an answer.

When I feed my method with : 1@com.c-

It returns true, but that's not a valid address, I wonder if anyone has a pattern to use in java ?

Frank

Ni_Mina at 2007-7-12 17:40:36 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 4

I once saw a regex that claimed to validate all e-mail addresses as specified in RFC 822. It was MUCH larger than what you have there... it took up about half my screen. And it was a Perl regex, so you would have to make a Java version of it (not sure what's involved there).

My opinion is that trying to validate e-mail addresses is a task not worth doing.

DrClapa at 2007-7-12 17:40:36 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 5
I see your point, but I just hope that JavaMail has a method to do that for us, it's easier for them to do it and provide it as a standard way in Java, if it can be done at all !Frank
Ni_Mina at 2007-7-12 17:40:36 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 6
In other words : new InternetAddress(Input).validate();is not doing a good enough job.Frank
Ni_Mina at 2007-7-12 17:40:36 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 7
If you really want to do it, then I suggest you should be satisfied with a second-rate solution. Does it really matter if you falsely accept "mike@banana.cd" -- which isn't valid because there's no "cd" top-level domain?
DrClapa at 2007-7-12 17:40:36 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 8

> I see your point, but I just hope that JavaMail has a

> method to do that for us, it's easier for them to do

> it and provide it as a standard way in Java, if it

> can be done at all !

There's a very precise syntax BNF specified for email

addresses. It's completely possible to write a parser

to verify address syntax. It's just work. And more work

than I wanted to do because, in the end, it doesn't really

tell you that much. The address might still be invalid.

It seemed simpler to just give the address to the mail

server and let it tell me if it thought the address was

invalid. Applications have to be prepared to handle

failures at that point in any event.

The address validation in JavaMail is "good enough"

for most uses. It can always be better. But I think it's

pretty close to the point of diminishing returns...

bshannona at 2007-7-12 17:40:36 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 9

Yes, for most users it's good enough, but suppose if my app needs user to enter an address for later use and I can ask him to reenter it to make sure the two are the same so it can store it in database, but if the app can't verify it's a good address, later when it needs to send email, the user is not there to verify the address again, then the email may never reach him. In this case, I want the checker to be as good as it can be to minimize the error rate without actually having to send him an email to verify the address.

Frank

Ni_Mina at 2007-7-12 17:40:36 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 10
You still have to handle the case where the user types a syntacticallyvalid address that is just wrong.If JavaMail's "good enough" is not good enough for your app, feel freeto write a more extensive address syntax verifier.
bshannona at 2007-7-12 17:40:36 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 11

Hi,

I had done the email validation but using my own written method.

private boolean checkEmail (String email)

{

int count=0;

char [] spcialchar = {'~','`','!','#','$','%','^','&','*','(',')','-','+','+','|','\\','[',']','{','}',':',';','"','\'','<','>',',','?','/',' '};

for(int i=0;i<email.length();i++)

{

if(email.charAt(i) == '@')

{

count++;

}

for(int j=0;j<spcialchar.length;j++)

{

if(email.charAt(i)==spcialchar[j])

{

count=2;

}

}

}

if(email.lastIndexOf(".")><email.indexOf("@"))

{

count=2;

}

String domain_name= email.substring(email.lastIndexOf(".")+1,email.length());

if(domain_name == null || domain_name.equals(""))

count=2;

if(count>1)

{

return false;

}

else

{

return true;

}

}

you can try this one.

bye......

RAMJANEa at 2007-7-12 17:40:36 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 12

The following was taken from http://64.233.167.104/search?q=cache:Z76rrgfIE5QJ:www.leshazlewood.com/%3Fp%3D5+java+email+address+regex&hl=en&ct=clnk&cd=3&gl=us

//RFC 2822 token definitions for valid email - only used together to form a java Pattern object:

private static final String sp = "\\!\\#\\$\\%\\&\\'\\*\\+\\-\\/\\=\\?\\^\\_\\`\\{\\|\\}\\~";

private static final String atext = "[a-zA-Z0-9" + sp + "]";

private static final String atom = atext + "+"; //one or more atext chars

private static final String dotAtom = "\\." + atom;

private static final String localPart = atom + "(" + dotAtom + ")*";

//one atom followed by 0 or more dotAtoms.

//RFC 1035 tokens for domain names:

private static final String letter = "[a-zA-Z]";

private static final String letDig = "[a-zA-Z0-9]";

private static final String letDigHyp = "[a-zA-Z0-9-]";

public static final String rfcLabel = letDig + letDigHyp + "{0,61}" + letDig;

private static final String domain = rfcLabel + "(\\." + rfcLabel + ")*\\." + letter + "{2,6}";

//Combined together, these form the allowed email regexp allowed by RFC 2822:

private static final String addrSpec = "^" + localPart + "@" + domain + "$";

//now compile it:

public static final Pattern VALID_PATTERN = Pattern.compile( addrSpec );

N.B. I did take one minor liberty with this regexp. You抣l notice in the domain definition, I limited the final token to be between 2 and 6 characters. Technically, the RFC says the end of the domain can be a rfcLabel, which can be 63 characters long. There are currently (at the time of this writing) only root level domains in the world between 2 and 6 characters long, inclusive. So, I added this for convenience and practicality.

Anyway, the above java code allows you to do things like the following.

In the EmailAddress class, you can have a method:

public static boolean isValid( String userEnteredEmailString ) { return VALID_PATTERN.matcher( userEnteredEmailString ).matches(); }

=====================

and here is another: http://regexlib.com/REDetails.aspx?regexp_id=711

Message was edited by:

zparticle

zparticlea at 2007-7-12 17:40:36 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 13
Great, nice to see your code, I've tried some addresses on both of our programs, 90% of the time yours is better, but in the case of something like this : ^@com.cnMy code flags it as false (because of the first char), yours says true.Frank
Ni_Mina at 2007-7-12 17:40:36 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 14
Hi Franck,you could use this link to perform email validation and so on understand what does it means. http://www.regular-expressions.info/regexbuddy/email.htmlJ閞鬽e
Baala at 2007-7-12 17:40:36 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 15
Hi Baal :I tried the following valid address : tom123@yahoo.comIt failed to pass the validator there.Frank
Ni_Mina at 2007-7-21 21:52:23 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...