I have an entity with a phoneNumber field. The format of this phone
number is +9912345678..., the international number notation.

What I want to do is to have a text field that accepts phonenumbers in
different formats. For example 0612345678, "06 12345678" but also
+31612345678. Based on the locale I want to 'normalize' 0612345678 to

I would like people to be able to enter a number in any format in a
form field but have the model behind that to be updated to the
international notation. (+NNNNNNNNNNNN)

I wrote a simple SimpleConverterAdapter implementation:

public class PhoneNumberConverter extends SimpleConverterAdapter
    public String toString(Object value)
        if (value instanceof String)
            String phoneNumber = (String) value;

            // Always match on an international phone number

            if (internationalPhoneNumberPattern.matcher(phoneNumber).matches())
                return phoneNumber;

            // Recognize dutch mobile numbers (06NNNNNNNN)

            if (dutchPhoneNumber.matcher(phoneNumber).matches()) {
                return "+31" + phoneNumber.substring(1);

        // If we cannot convert the object then throw an

        throw new IllegalArgumentException("Cannot convert non String objects");

    public Object toObject(String value)
        return value == null ? null : value.toString();

but the behaviour is very odd. When I enter '0612345678' in the form
and submit it, I get an error message saying that 0612345678 is not a
valid phone number (this is done by the PhoneNumberValidator) but the
phone number form field is updated to +316512345678. When I press
submit again then all is ok of course.

I'm not sure what I'm doing wrong here. I simply want to accept input
in several formats.


Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
Wicket-user mailing list

Reply via email to