Run libphonenumber javascript version in blackberry app

So this is what happened, in my blackberry app, I want to recognize a country by parsing phone number. For example, from +8285659XXXXXX I expect to get country is South Korea and phone number is 85659XXXXXX. The challenge is to cover all 240 more country code and verify if the number is a valid one. My first thought is to compare all the first 1-4 number to list of country code as shown here. Lucky for us, google has release their phone number parsing library libphonenumber. This library is released in C++, java and javascript. Since blackberry development is in java, maybe we can use java version? Unfortunately not. Blackberry java is J2ME and to run libphonenumber you need whole deal of java. For example, java.util.Map and

phone number validation

phone number validation

Then the options are either to port the code to J2ME compatible, or execute the javascript version. Yes, you can execute javascript code programmatically using BrowserField class from Blackberry JDE. If you use BlackBerry Java Plug-in for Eclipse, like me, you can find sample application that integrate java code with javascript.  You can test libphonenumber javascript demo here. And you can get the source code using svn.

So how do we do this ?

First, we need to modify libphonenumber  javascript demo code to get parameter from url and return string output. Because, user selected phone number from his contact list and we don’t want them to enter the number again to, say, a textbox. We will use parameter in url, for example


To get the parameter, we can use nice code to query url string from here.

Next, just pass the number we get from url to the javascript function inside our html code.

var result = "none";
function phoneNumberParser() {
var phoneNumber = gup( 'phoneNumber' );
try {
var phoneUtil = i18n.phonenumbers.PhoneNumberUtil.getInstance();
var number = phoneUtil.parseAndKeepRawInput(phoneNumber, regionCode);
return result;

Set the html and necessary javascript files from libphonenumber to our project resource.

Then in our blackberry java code, we execute javascript code.

public class ContactListScreen extends MainScreen
private BrowserField _browserField;

// screen constructor, set browserfield listener so we can execute our javascript code when html document loaded
public ContactListScreen(Country country)
BrowserFieldConfig config = new BrowserFieldConfig();
config.setProperty(BrowserFieldConfig.ALLOW_CS_XHR, Boolean.TRUE);
_browserField = new BrowserField(config);
BrowserFieldListener listener = new BrowserFieldListener()
public void documentLoaded(BrowserField browserField, org.w3c.dom.Document document) throws Exception
String strReturnValue = (String)browserField.executeScript("javascript:phoneNumberParser()");;
catch(Exception ex)
{"Phonenumber validation error : " + ex.getMessage());
_browserField.addListener( listener );

// run number validation
private void validateNumber(String input)
BrowserFieldRequest request = new BrowserFieldRequest("local:///index.html?phoneNumber=" + input + "&defaultCountry=ID");


That’s it. Note that you need to be always connected to internet, since libphonenumber use googles’s closure library. But I think you can run it locally, by copying all necessary files from closure into one folder and include those javascript file in your html. Have fun 🙂


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: