• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Email Validation code

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . and welcome to JavaRanch
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"yes boss",

Please check your private messages.

Thanks,

Rob
 
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]*");
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic