Friday, November 4, 2011

Blackberry JDE: Validate Email Address With Regex

There is no build in solution in the Blackberry JDE to validate an Email address. A common approach would be to use regular expressions with a Pattern Match approach.

Unfortunately the Pattern and Match classes are also missing in the JDE.

There is a third party Regex project for Java ME called Regexp-me.
http://code.google.com/p/regexp-me/
Check out the source files and integrate them into your Blackberry Java project.

Below example describes how to utilise the RE class.

public static boolean validateEmail(String email) {
     if(email.length() == 0) {
         return false;
     }

     String pttn = "^\\D.+@.+\\.[a-z]+";
     RE regex = new RE(pttn);
    
     if(regex.match(email)) {
         return true;
     }

     return false;
 }

That's it. Let me know if you have any problems with the instructions.

0 comments:

Post a Comment