aspose file tools*
The moose likes Beginning Java and the fly likes Email validation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Email validation" Watch "Email validation" New topic
Author

Email validation

govind soni
Greenhorn

Joined: Dec 07, 2010
Posts: 11
Could anybody help me out to solve the problem for email validation code in java, but without using any java inbuilt method like Macher class etc.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19544
    
  16

Why do you want to reinvent the wheel? Whenever I need to validate email addresses I just use javax.mail.internet.InternetAddress.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
govind soni
Greenhorn

Joined: Dec 07, 2010
Posts: 11
:-D :-D Actually this task is asign to me by my manager. so help me out to find out the differ way without any API .
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
Your manager specifically tells you which classes you can and can not use? Seems like a micro-managing PHB who should get out of your way.

Or you could use the source code of http://commons.apache.org/validator/api-1.4.0/org/apache/commons/validator/routines/EmailValidator.html and pretend you wrote it. Of course, it may well use the class Rob mentioned.
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1106

govind soni wrote::-D :-D Actually this task is asign to me by my manager. so help me out to find out the differ way without any API .


Is this an assesment?
Kotto Bass
Greenhorn

Joined: Jan 27, 2006
Posts: 17
Just use a regular expression. 'google' it
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Tim Moores wrote:Your manager specifically tells you which classes you can and can not use? Seems like a micro-managing PHB who should get out of your way.


To me it sounds more like a student falsely claiming that homework is a work assignment. No real manager--even in the worst hellhole outsourcing shop ever--would ever forbid you from using the core API. And if he does (which he never will), you should run as fast as you can. Cleaning toilets in a bus station in the slums of hell would be a better job than that.
govind soni
Greenhorn

Joined: Dec 07, 2010
Posts: 11
:-D :-D
govind soni
Greenhorn

Joined: Dec 07, 2010
Posts: 11
Check this code I tried myself but this is not showing correct email validation..... try by yourside and tell me the updates.

import java.lang.*;
import java.io.*;

public class EmailV{
public static void main(String[] args) throws IOException{
System.out.println("Email Validation");
BufferedReader bf =
new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please enter mailid:");
String str1 = bf.readLine();
String myString="str1";
String at="@";
String dot=".";
int lstr = myString.length();
int lat = myString.indexOf("at");
int ldot = myString.indexOf("dot");



{


if (myString.indexOf(at)==-1 || myString.indexOf(at)==0 ||
myString.indexOf(at)==lstr)
System.out.println("Invalid E-mail ID");

else

if (myString.indexOf(dot)==-1 || myString.indexOf(dot)==0 ||
myString.indexOf(dot)==lstr)
System.out.println("Invalid E-mail ID");

else

if (myString.indexOf(at,(lat+1))!=-1)
System.out.println("Invalid E-mail ID");

else

if (myString.substring(lat-1,lat)==dot ||
myString.substring(lat+1,lat+2)==dot)
System.out.println("Invalid E-mail ID");

else

if (myString.indexOf(dot,(lat+2))==-1)
System.out.println("Invalid E-mail ID");

else


if (myString.indexOf(" ")!=-1)
System.out.println("Invalid E-mail ID");

else


System.out.println("valid E-mail ID");
}
}
}
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18120
    
  39

govind soni wrote:Check this code I tried myself but this is not showing correct email validation..... try by yourside and tell me the updates.



My first update would be to recommend that you add some debugging messages -- this will enable you to figure out what is going on, and hence, be able to fix it. This way, when you come back here, you can ask specific questions, and with specific details.

My second recommendation is to use the equals() method to compare strings. The "==" operator is used to compare references, which is not what you want.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Henry Wong wrote:This way, when you come back here, you can ask specific questions, and with specific details.


And when you do come back and ask specific questions with specific details, make sure you're clear about what your actual requirements are. "Valid email address" can have many different interpretations (Read this page for some examples.) and in the absence of specific requirements for your particular case, the only reasonable default would be RFC 2822, and I doubt you actually have to implement conformance to that.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Email validation
 
Similar Threads
jQuery select option
difference between and validate() and validation.xml
Please share any Java code to do email validation
When to use Validation Method and when to use Validation Framework
How to validate email id with special characters + Validation FrameWork