문제

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.

도움이 되었습니까?

해결책

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() || "" })
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top