This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please share any Java code to do email validation

 
Abhay Agarwal
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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
 
Balasubramanian Chandrasekaran
Ranch Hand
Posts: 215
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • 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.
 
Uli Hofstoetter
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There has recently been a discussion on this topic:
Email Validation code

Regards,
Uli
 
Ramya Chowdary
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • 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.....
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Campbell Ritchie
Sheriff
Pie
Posts: 50277
80
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
hi Campbell

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

Thanks
Abhay Agarwal
 
john emmanuel
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • 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
  • 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: 42968
73
  • Mark post as helpful
  • send pies
  • 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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic