• 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

Please share any Java code to do email validation

 
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There has recently been a discussion on this topic:
Email Validation code

Regards,
Uli
 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.....
 
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
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 ]
 
Abhay Agarwal
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Campbell

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

Thanks
Abhay Agarwal
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
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
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
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Abhay Agarwal
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
reply
    Bookmark Topic Watch Topic
  • New Topic