The Artima Developer Community
Sponsored Link

Java Answers Forum
validating a email address

3 replies on 1 page. Most recent reply: Dec 9, 2003 4:43 AM by sudheesh

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
bernard

Posts: 6
Nickname: homer
Registered: Nov, 2003

validating a email address Posted: Dec 5, 2003 11:31 AM
Reply to this message Reply
Advertisement
I have written the following methos for validating a email , but i've tried it and it's wrong. CAn any One help me out here?


boolean isAddressValid(String address){
int opPos=address.indexOf("@");
if(opPos==-1){//no @
return(true);
}
if(address.indexOf("@",opPos)!=-1){//more then one @
return(true);
}

if(address.indexOf(".")<opPos){//checks that there are no dots //before the @
return(true);
}
if(address.indexOf(".",opPos)== -1){//checks that there is at least one dot after the @
return(true);

}


return(false);//if none of the flags rose up it means that the email is valid

}


Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: validating a email address Posted: Dec 6, 2003 1:44 AM
Reply to this message Reply
I suggest you look at Regular Expressions specially in String Class. There is a method match() in String class in JDK 1.4 and using regular expressions to validate email address is much simple and only one line of code.

Adam Duffy

Posts: 168
Nickname: adamduffy
Registered: Feb, 2003

Re: validating a email address Posted: Dec 8, 2003 4:09 AM
Reply to this message Reply
See RFC 822 for e-mail address standard.

http://www.w3.org/Protocols/rfc822/rfc822.txt

Of particular interest to you might be section 6.

Adam

sudheesh

Posts: 4
Nickname: sudhee
Registered: Dec, 2003

Re: validating a email address Posted: Dec 9, 2003 4:43 AM
Reply to this message Reply
Shouldnt the check
if(address.indexOf("@",opPos)!=-1){//more then one @

be changed something like
if((address.substring(opPos+1).indexOf("@"))!=-1){//more then one @

Flat View: This topic has 3 replies on 1 page
Topic: Garbage Collection in Multithreaded App Previous Topic   Next Topic Topic: multiple heaps

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use