dcsimg
Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


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

WEBINAR:

On-Demand


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.

 

 

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