PDA

View Full Version : Phone Number renderer



frew
2 Apr 2009, 12:42 PM
Hey all,

We are slowly building up a collection of renderers and I just made one that I thought some people might find useful. Feel free to make it better etc!



ACDRI.fn.Renderers.phone = function(value) {
var arr = value.split('');
if (arr.length == 7) {
first_part = arr.splice(0, 3);
return first_part.join('') + '-' + arr.join('');
} else if (arr.length == 10) {
first_part = arr.splice(0, 3);
second_part = arr.splice(0, 3);
return "(" + first_part.join('') + ") " + second_part.join('') + '-' + arr.join('');
} else {
return value;
}
}


so 1234567 -> 123-4567, 1234567890 -> (123) 456-7890, 549803409 -> 549803409.

Ideas for expansion: use a regex to only format numbers that have only numbers. Add support for extensions.

Enjoy!

poppabear
2 Apr 2009, 2:24 PM
Thanks for the snippet .... Works great.