File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Email Validation code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Email Validation code" Watch "Email Validation code" New topic

Email Validation code

Kumar Gaurav
Ranch Hand

Joined: Apr 08, 2008
Posts: 108
Hi All,
i have written a code to validate an email ID which is as follows:
import java.util.regex.*;

public class EmailValidation{
public static void main(String []args){
String email="";
Pattern p=Pattern.compile("[a-zA-Z]*[0-9]*@[a-zA-Z]*.[a-zA-Z]*");
Matcher m=p.matcher(email);
//Matcher m=p.matcher(args[0]);
boolean b=m.matches();
System.out.println("Valid Email ID");
System.out.println("InValid Email ID");
If i m using email as either or
Its giving invalid EmailID.Kindly let me know how to deal i.
Kumar Gaurav

Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

Why not use the address validation parsing that is included in the javax.mail.internet.InternetAddress class? Save you reinventing the wheel. (Particularaly as SMTP addresses don't need to have things like an @ symbol to be valid).

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 45271
. . . and welcome to JavaRanch
Rob Spoor

Joined: Oct 27, 2005
Posts: 20180

"yes boss",

Please check your private messages.



How To Ask Questions How To Answer Questions
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
And I'll toss out there (because someone has to),

there is no guarantee that a "valid email address" is valid,
until you email something to it and receive a response,

Bill Shirley - bshirley -
if (Posts < 30) JavaRanchFAQ);
Pat Farrell

Joined: Aug 11, 2007
Posts: 4659

Right, you can only validate that the form is RFC822 compliant. That means the form is proper. Not that there is a host and user to receive it.
Uli Hofstoetter
Ranch Hand

Joined: Nov 24, 2006
Posts: 57
Originally posted by yes boss:

Pattern p=Pattern.compile("[a-zA-Z]*[0-9]*@[a-zA-Z]*.[a-zA-Z]*");

If i m using email as either or

These two examples simply don't match your pattern. Your pattern says "zero or more letters, followed by zero or more digits, follwed by @, followed by zero or more letters, follwoed by any character (note that the . is a special char in regexp, to match the char '.', it must be escaped '\.'), followed by zero or more letters.

I guess you should rethink your pattern or dive deeper into regexp to learn how to use them correctly.

Besides the options already mentioned, you could look at the commons-validator package from apache to validate the correct form of an email adress (and many more validations).

Kind regards,
[ May 21, 2008: Message edited by: Uli Hofstoetter ]

SCEA5, Certified ScrumMaster
Jyothis K.S.

Joined: Sep 20, 2008
Posts: 1
it is not working is i give input as "abcd@dmail" instead of "".
it is not validating the last portion
Rob Spoor

Joined: Oct 27, 2005
Posts: 20180

"Jyothis K.S.",

Please read your private messages regarding an important announcement.

Thank you,


That said, I'd just take Paul's advice and let InternetAddress do the hard work for you.
Jp Rai

Joined: May 27, 2011
Posts: 2
use below one, it will work.

Pattern p=Pattern.compile("[a-zA-Z]*[0-9]*@[a-zA-Z]*\\.[a-zA-Z]*");
Jelle Klap

Joined: Mar 10, 2008
Posts: 1948

You really shouldn't disallow an e-mail adress based on regular expression matching, especially one you wrote yourself.
At most, you could use a regular expression to hint to the user that the e-mail adress might not be valid.
Have a look at this regex, which is used by a Perl module to validate against RFC822 (superseded by RFC2822).
Just, don't.

Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
I agree. Here's the link:
subject: Email Validation code
jQuery in Action, 3rd edition