Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show formatted contact numbers?

I have a field called phone number which has values such as 0833888999 that I want to format as following 0833 888 999.

The answer that Rachana offered blew is true for few countries but not all countries.

Therefore, I am using this Google library to format contact numbers from different countries; however, the problem is that after persisting the contact numbers in database I can not search for them on database, for example the library would format contact numbers of different countries in different format, for example add space or "-" between them that makes hibernate unable to find them.

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

Hibernate

 .add(Restrictions.ilike("user.phone","+18182223333"); 
like image 756
Jack Avatar asked Mar 20 '23 03:03

Jack


2 Answers

Try this

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

Where,phone=0833888999

Hope this will help you also see Using Struts2 Tags to Formatting Numbers

you will get clear idea about number formatting

like image 175
rachana Avatar answered Mar 21 '23 16:03

rachana


I think you should keep the raw phone number (e.g. 0833888999) in the database and it's the View responsibility to format it accordingly.

You can have a separate table "country_phone_format" holding a country_id and a phone_format and you could fetch the phone_format as a @ManyToOne entity so that you have both the raw data and the format to properly display it into the View.

The PhoneFormat could be easily cached with the second level cache, as they should be rarely modified.

like image 22
Vlad Mihalcea Avatar answered Mar 21 '23 15:03

Vlad Mihalcea