Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Email Validation code

 
Kumar Gaurav
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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="gaurav123@xyz.com";
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();
if(b==true)
{
System.out.println("Valid Email ID");
}
else
{
System.out.println("InValid Email ID");
}
}
}
If i m using email as either 13gaurav@xyz.com or gaurav_123@xyz.com
Its giving invalid EmailID.Kindly let me know how to deal i.
Thanks,
Kumar Gaurav
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Campbell Ritchie
Sheriff
Pie
Posts: 50258
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and welcome to JavaRanch
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"yes boss",

Please check your private messages.

Thanks,

Rob
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 13gaurav@xyz.com or gaurav_123@xyz.com


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,
Uli
[ May 21, 2008: Message edited by: Uli Hofstoetter ]
 
Jyothis K.S.
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it is not working is i give input as "abcd@dmail" instead of "abcd@dmail.com".
it is not validating the last portion
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Jyothis K.S.",

Please read your private messages regarding an important announcement.

Thank you,

Rob


That said, I'd just take Paul's advice and let InternetAddress do the hard work for you.
 
Jp Rai
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use below one, it will work.

Pattern p=Pattern.compile("[a-zA-Z]*[0-9]*@[a-zA-Z]*\\.[a-zA-Z]*");
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic