GeeCON Prague 2014*
The moose likes Java in General and the fly likes Please share any Java code to do email validation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Please share any Java code to do email validation" Watch "Please share any Java code to do email validation" New topic
Author

Please share any Java code to do email validation

Abhay Agarwal
Ranch Hand

Joined: Feb 29, 2008
Posts: 1106
    
    1

Hi

I have to do Email Address verification in .java file.

Please share any Java code to do email validation.

I have to set following validation rules for Email address

oIt should have @ sign
oIt should at least one period (.) after �@� sign.
oThere should not be any spaces


Thanks in advance
Abhay Agarwal


Oracle Java Web Service Developer (1z0-897), Oracle certified Java 7 Programmer, SCJA 1.0, SCJP 5.0, SCWCD 5.0, Oracle SQL Fundamentals I
Balasubramanian Chandrasekaran
Ranch Hand

Joined: Nov 28, 2007
Posts: 215

Originally posted by Abhay Agarwal:
Hi

I have to do Email Address verification in .java file.



Thats where you can use Regular Expressions. As a beginner you may feel hard dealing with Regular Expressions. Check out this link, it gives the introduction for using Regular Expression in java.
Uli Hofstoetter
Ranch Hand

Joined: Nov 24, 2006
Posts: 57
There has recently been a discussion on this topic:
Email Validation code

Regards,
Uli


SCEA5, Certified ScrumMaster
Pratap koritala
Ranch Hand

Joined: Sep 27, 2006
Posts: 252
get the reqular expression for email , ( a simple google search away or you can get from any site which does email validation)

Then match it.....
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Or use the JavaMail API as suggested in the thread Uli linked to and have all the hard work done for you - including extensive testing.
[ May 30, 2008: Message edited by: Rob Prime ]

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Abhay Agarwal
Ranch Hand

Joined: Feb 29, 2008
Posts: 1106
    
    1

Hi

Thanks to all of you for your reply.

Thanks for guiding me

I have made a regular expression which solve my problem for Email Validation. That regular expression takes any pattern of email.

Thanks
Abhay
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39096
    
  23
Originally posted by Abhay Agarwal:
Hi

Thanks to all of you for your reply.
I have made a regular expression which solve my problem for Email Validation. That regular expression takes any pattern of email.
Well done Please show us what you are using so everybody can learn from it.
Abhay Agarwal
Ranch Hand

Joined: Feb 29, 2008
Posts: 1106
    
    1

hi Campbell

Sure .. i will share my Email Validation Code soon...

Thanks
Abhay Agarwal
john emmanuel
Greenhorn

Joined: Apr 18, 2008
Posts: 5
You should read class String in java.lang
check the java documentation.
You could use methods like '.contains("@");' to check if the email got contains an @ sysmbol.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

If only that were the only requirement it would be so very easy. That would also pass "@@" - surely not a valid email address.

I've seen a quite complete regular expression for email addresses - it didn't even fit on one screen.
Abhay Agarwal
Ranch Hand

Joined: Feb 29, 2008
Posts: 1106
    
    1

Hi

Here is the sample code which uses Regular Expressions for Email Adresss Validation.

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

import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class EmailAddressValidation {
public static void main(String[] args) {
emailaddressvalidation("abc.xyz@qwerty.com");
emailaddressvalidation("abc.xyz@qwerty.com.");
emailaddressvalidation("!!!abc_xyz@qwerty.com");
emailaddressvalidation(".abc.xyz@qwerty.com");
emailaddressvalidation("abc.xyz.2008@qwerty.com");
emailaddressvalidation("abc.xyz@qwerty.com");
}
public static void emailaddressvalidation(String email)
{
email=email.trim();

// Here is the Regular Expression for Email Address validation
Pattern p=Pattern.compile("(\\w+\\.)*\\w+@(\\w+\\.)+[A-Za-z]+");

Matcher m=p.matcher(email);
boolean b=m.matches();
if(b==true)
{
System.out.println("=======================================================");
System.out.println("Email ID = " + email + " : It is a VALID email address");
}
else
{
System.out.println("=======================================================");
System.out.println("Email ID = " + email + " : It is INVALID email address");

}

}

}
====================================================

Sample Output :-


=======================================================
Email ID = abc.xyz@qwerty.com : It is a VALID email address
=======================================================
Email ID = abc.xyz@qwerty.com. : It is INVALID email address
=======================================================
Email ID = !!!abc_xyz@qwerty.com : It is INVALID email address
=======================================================
Email ID = .abc.xyz@qwerty.com : It is INVALID email address
=======================================================
Email ID = abc.xyz.2008@qwerty.com : It is a VALID email address
=======================================================
Email ID = abc.xyz@qwerty.com : It is a VALID email address

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

Thanks
Abhay
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42061
    
  64
Are you sure that ".abc.xyz@qwerty.com" is an invalid address? If dots can occur before the "@", why couldn't they occur at the beginning of the address?

The regexp I generally use (after lowercasing the address) is "^\\S+@([-\\w]+\\.){1,4}[a-z]{2,6}$". IMO, it's better to allow some invalid addresses than to reject some valid ones - few things will annoy a customer faster than having a valid email rejected.

The code also checks that there is no more than a single "@" character, and that the TLD is either 2 characters (for country TLDs), or one of: "aero", "arpa", "asia", "biz", "cat", "com", "coop", "edu", "gov", "info", "int", "jobs", "mobi", "mil", "museum", "name", "net", "org", "pro", "tel", "travel". That list occasionally needs expanding, of course.
[ June 03, 2008: Message edited by: Ulf Dittmer ]

Ping & DNS - my free Android networking tools app
Abhay Agarwal
Ranch Hand

Joined: Feb 29, 2008
Posts: 1106
    
    1

Hi Ulf

I tried making a new email id on Gmail.com as ".abhay2008@gmail.com" but it gave an error message that "first character should be ascii or numeric".

Thanks
Abhay
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42061
    
  64
Note that it says "should". Just because Google won't allow you to create a certain email address doesn't mean it is invalid, or that someone somewhere won't have one like that. I suggest you read the relevant RFCs that specify what is and is not a valid email address.
[ June 04, 2008: Message edited by: Ulf Dittmer ]
 
GeeCON Prague 2014
 
subject: Please share any Java code to do email validation