Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Java
Expertise: Intermediate
Jul 14, 2009

Correction to the Tip "Email Address Validation Using a Regular Expression"

There tip corrects a problem with the previously published tip Email Address Validation Using a Regular Expression. The problem with the original version is that when you enter an email address such as (for example) aaa@ddd.c, the the function incorrectly returns true. The problem is in the country code. Here's a corrected version:

public boolean validateEmail(email){
   // Input the string for validation
   // String email = "xyz@.com";
   // Set the email pattern string
   Pattern p = Pattern.compile(".+@.+\\.[a-z]+");

   // Match the given string with the pattern
   Matcher m = p.matcher(email.getText());

   // check whether match is found
   boolean matchFound = m.matches();

   StringTokenizer st = new StringTokenizer(email, ".");
   String lastToken = null;
   while (st.hasMoreTokens()) {
      lastToken = st.nextToken();

   if (matchFound && lastToken.length() >= 2
      && email.length() - 1 != lastToken.length()) {

      // validate the country code
      return true;
   else return false;
Bennaidja Billel
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date