A Custom Parser for Comma-delimited Numbers with jQuery Tablesort

I was using jQuery tablesort on a recent project and I found that it was treating comma-delimited numbers as plain strings. An example of a comma-delimited number is: 99,001. This was a deal-breaker for me, so I looked at the documentation and I came up with the following custom parser. You can add this to the Javascript on your webpage and it will recognize comma-delimited numbers the same way it recognizes plain numbers:


jQuery.tablesorter.addParser({
  id: "fancyNumber",
  is: function(s) {
    return /^[0-9]?[0-9,\.]*$/.test(s);
  },
  format: function(s) {
    return jQuery.tablesorter.formatFloat( s.replace(/,/g,'') );
  },
  type: "numeric"
});
		

Place this inside a document.ready and you are good to go:


jQuery(document).ready( function() { 
	/* here! */
} );