• Resolved mixandmatchrecipes

    (@mixandmatchrecipes)


    I want to render a table which has a column for phone numbers. Is it possible for the table to format the entry in the column to display it as a phone number?
    For example, if I enter 4255556666, it should display it as 425 555-6666 or 425-555-6666.

    Thanks,
    Kalpana

    The page I need help with: [log in to see the link]

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author TobiasBg

    (@tobiasbg)

    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

    Thread Starter mixandmatchrecipes

    (@mixandmatchrecipes)

    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

    Thread Starter mixandmatchrecipes

    (@mixandmatchrecipes)

    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

    Plugin Author TobiasBg

    (@tobiasbg)

    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!

    Thread Starter mixandmatchrecipes

    (@mixandmatchrecipes)

    Thanks, Tobias.
    PS: I left a review. ??

    Plugin Author TobiasBg

    (@tobiasbg)

    Hi,

    thanks, I really appreciate it!

    Best wishes,
    Tobias

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Phone number formatting’ is closed to new replies.