Phone number formatting | WordPress.org

Hi,

thanks for your post, and sorry for the trouble.

For this, you could try using some custom JavaScript code (you will need to enable the “Use DataTables” checkbox on the table’s “Edit” screen for this to work, if it’s not yet active).
Then, please add this to the “Custom Commands” text field on the table’s “Edit” screen:

"columnDefs": { [ "targets": [ 3 ], "render": function ( toFormat ) { return toFormat.toString().replace( /(\d{3})(\d{3})(\d{4})/g, '$1-$2-$3' ); } ] }

That 3 after targets would have to be adjusted the column number minus 1, of the column with the phone numbers. So, this example, with the 3 would apply this to the fourth column of the table.

Regards,
Tobias

Hi Tobias.

I enabled ‘Use DataTables’ and added the custom command. My column is the 3rd column in the table, so I changed the ‘3’ after targets to 2. But, unfortunately, I see no difference. It is still rendering the phone number as I type it. If I type 4253334444, it just renders the same.

Thanks,
Kalpana

Hi Tobias,

I found the issue. The Custom Command string should be
‘columnDefs’: [{ “targets”: [ 2 ], “render”: function ( toFormat ) { return toFormat.toString().replace( /(\d{3})(\d{3})(\d{4})/g, ‘$1-$2-$3’ ); } }]

instead of
“columnDefs”: { [ “targets”: [ 3 ], “render”: function ( toFormat ) { return toFormat.toString().replace( /(\d{3})(\d{3})(\d{4})/g, ‘$1-$2-$3’ ); } ] }

It started working after I made the change.

Thanks,
Kalpana

Hi,

ah, totally right! I mixed up those { and ]… Sorry about that!
Great to hear that it’s working now! 🙂

Best wishes,
Tobias
 
P.S.: In case you haven’t, please rate TablePress here in the plugin directory. Thanks!

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Your Advertisement Here