| Author |
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="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
|
Regards,
Gaurav
|
 |
Paul Sturrock
Bartender
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
Sheriff
Joined: Oct 13, 2005
Posts: 32654
|
|
. . . and welcome to JavaRanch
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
"yes boss", Please check your private messages. Thanks, Rob
|
SCJP 1.4 - SCJP 6 - SCWCD 5
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 - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
|
 |
Pat Farrell
Rancher
Joined: Aug 11, 2007
Posts: 4422
|
|
|
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
|
|
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 ]
|
SCEA5, Certified ScrumMaster
|
 |
Jyothis K.S.
Greenhorn
Joined: Sep 20, 2008
Posts: 1
|
|
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
Joined: Oct 27, 2005
Posts: 19216
|
|
"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
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
Bartender
Joined: Mar 10, 2008
Posts: 1407
|
|
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.
|
 |
 |
|
|
subject: Email Validation code
|
|
|