How to show formatted contact numbers?

I have a field called a phone number that has values ​​like 0833888999, which I want to format as the next 0833 888 999.

The answer proposed by Rachana was right for several countries, but not for all countries.

Thus, I use this Google library to format contacts from different countries; however, the problem is that after saving contact numbers in the database, I can’t search for them in the database, for example, the library will format the contact numbers of different countries in a different format, for example, add a space or “-” between them, which makes the sleeping mode could not find them.

+18182223333 >>> +1 818-222-3333
+441135558888 >>> +44 113 555 8888

Hibernate

 .add(Restrictions.ilike("user.phone","+18182223333"); 
+4
source share
3 answers

try it

<s:property value="getText('{0,number,0### ### ###}',{phone})"/>

Where phone=0833888999

Hope this helps you also see Using Struts2 tags to format numbers

you get a clear idea of ​​number formatting

+3
source

I think you should save the raw phone number (for example 0833888999) in the database, and that means responsible for formatting it.

"country_phone_format" , country_id phone_format, phone_format @ManyToOne , , .

PhoneFormat , .

+2

, (, 0833888999) ( , ), .

You can use Type Conversion to convert to / from the desired format - and use the google library you mentioned. Something like the following might get you started (in short, to get the gist of it):

public class MyConverter extends StrutsTypeConverter {
   public Object convertFromString(Map context, String[] values, Class toClass) {
      String phoneNr = values[0]
      MyPhoneObject phone = googleLibrary.doYourMagic(phoneNr);
      return phone;
   }

   public String convertToString(Map context, Object o) {
      googleLibrary.parseString( ((MyPhoneObject)o).rawPhoneNr() );
   }
}

Do not forget to register the converter in xwork-conversion.properties

my.PhoneObject=path.to.MyConverter
+2
source

Source: https://habr.com/ru/post/1547097/


All Articles