December 16, 2015

Quick Phone Number formating

JavaScriptPhone.jpg

Today's TextExpander tip is a simple tool to convert a phone number into a more readable type of format.

Every once in a while I'll need to convert a nine digit number such as 8004249090 to an easier readable format such as (800) 424-9090.

This is the Javascript snippet that I use in my TextExpander library:

var pnum ="%clipboard";
    var numbers = pnum.replace(/D/g, ''),
        char = {0:'(',3:') ',6:' - '};
    pnum = '';
    for (var i = 0; i < numbers.length; i++) {
        pnum += (char[i]||'') + numbers[i];
    }

I call my abbreviation .pformat.

So, now when I encounter the nine digit number, I can simply copy the number and then type in .pformat and POOF! the format appears.

If you know some basic JavaScript, you can easily change up the function above to make the number display any way you want.

Comments

Add your Comments

Feel free to leave a comment about this post.

- Feel Free to add HTML to your comment!