Phone number

Make phone numbers searchable formatted or unformatted

  • Author: Allan Jardine

Telephone numbers are a common data point to display in HTML tables, and are often formatted (e.g. 555-1234). Typically, when searching a table a user would need to enter the number in exactly the same format it is displayed in, but this is not always convenient (e.g. you might search for 5551).

This filtering plug-in will allow both forms to be matched be providing both the formatted and de-formatted data to the table's search.

Plug-in code

jQuery.fn.DataTable.ext.type.search.phoneNumber = function ( data ) {
    return ! data ?
        '' :
        typeof data === 'string' ?
            data + data.replace(/[ \-]/g, '') :
            data;
};

CDN

This plug-in is available on the DataTables CDN:

JS

Note that if you are using multiple plug-ins, it is beneficial in terms of performance to combine the plug-ins into a single file and host it on your own server, rather than making multiple requests to the DataTables CDN.

Version control

If you have any ideas for how this plug-in can be improved, or spot anything that is in error, it is available on GitHub and pull requests are very welcome!

Example

$(document).ready(function() {
    $('#example').dataTable( {
      columnDefs: [
        { type: 'phoneNumber', target: 4 }
      ]
    } );
  } );