Question

How to format a number as phone number (e.g. (45)879-2658 ) using MS Ajax formatting.

In C#, i can format this number using {0:(###)###-####} .

But not sure how to achieve this format in MS Ajax for an integer.

Was it helpful?

Solution

I don't know C#, this is how you can do picture formatting in javascript:

format = "(###)###-####"
input = 1234567890
formatted = format.replace(/#/g, [].shift.bind(String(input).split("")))
// result: "(123)456-7890"

To handle strings shorter than a picture, try this slightly more verbose code:

chars = String(input).split("")
formatted = format.replace(/#/g, function() { return chars.shift() || "" })
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top