Web Hosting Talk







View Full Version : Java Help and Free-Email Domain Blocking


MattF
07-02-2001, 11:17 AM
Anyone got a java class/function/method for validating an e-mail address (just checking in form of user@domain.tld)???

Also has anyone got a text file,xml file, or even a database of free e-mail domains, I want to block free e-mail domains in an upcoming project. I started creating my own in notepad but gave up, there are too many.

ckpeter
07-02-2001, 06:29 PM
Here you go, Matt,

<pre>
public static boolean isValidEmail(String email)
{
try
{
int index = email.indexOf('@');

//separate into "user" and "domain.tld"
String username = email.substring(0, index);
String domain = email.subString(index + 1, email.length());

//check username
if(username.length() == 0) return false;

char[] chars = username.toCharArray();

//loop to check every letter
for(int k = 0; k < chars.length; k++)
{
if(!Charactor.isLetterOrDigit(chars[k]))
{
if(chars[k] != '.')
{
return false;
}
}
}

//check domain
if(domain.length() == 0) return false;

//reusing chars
chars = domain.toCharArray();

//loop to check every letter
for(int k = 0; k < chars.length; k++)
{
if(!Charactor.isLetterOrDigit(chars[k]))
{
if(chars[k] != '.')
{
return false;
}
}
}

return true;
}
catch(Exception e)
{
//there was probably some index problem
return false;
}

}
</pre>

make sure you test the code out, I did not do any test.

Peter

ckpeter
07-02-2001, 06:33 PM
<pre>
stupid vBulletin ... messed up my formatting...
</pre>

Peter