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
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.