Saturday, February 26, 2011

J2ME Phone Number as Integer

In my current project, we used a mobile application based on Openxdata/Epihandy (forked earlier), which is an xForms J2ME app.

One of the problems encountered was entering phone numbers in a form field.
The closest type supported was a numeric type, using the MIDP Textfield NUMERIC contraint.
As the doc states, this value should be parseable by CLDC Integer,parseInt()
The Nokia series 40 phones we were using, Nokia 1680s, presented various issues during the project including editing a numeric Textfield used for a phone number.
The original phone number value entered could be larger than (or equal to) the max Integer value (2147483647), but the value would be lost when trying to edit the field.
On editing, the value would fail to parse as an Integer, throwing a NumberFormatException, and default to an empty Textfield.
As a workaround, we added validation to only allow values less than the max integer value.

This solution worked fine for our testing using Maine area code 207 phone numbers, and in production entering local phone numbers (9 digits) on the phone with the country code added on the server.
But this approach fails for almost all other US area codes (most greater than 214-748-3647) including the one I needed to support for a demo.

The MIDP Textfield supports a phone number constraint, which I added in our mobile app as a fix/feature. I reviewed the existing code and found there were just a few case codings needed. For the xForm, the type is actually string, but includes a format attribute with the value phonenumber, similar to how other added types are handled.

This has been a lingering limitation and I'm happy to see it being resolved. We've used phone number fields on almost all forms, especially since we've sent text messages to numbers entered. Obviously the numeric Textfield was insufficient for phone numbers.
Now the server side just needs to account for the possible + prefix instead of numeric only validation. To provide a consistent US phone number format, I used the regular expression \\+?1?([0-9]{10}) and selected the matching group.

No comments:

Post a Comment