Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Email validation

 
govind soni
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20492
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you want to reinvent the wheel? Whenever I need to validate email addresses I just use javax.mail.internet.InternetAddress.
 
govind soni
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
:-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
Bartender
Posts: 2674
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1110
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just use a regular expression. 'google' it
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
:-D :-D
 
govind soni
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20880
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic