0

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.

RGR
  • 1,521
  • 2
  • 22
  • 36
  • I'm not sure I follow. What do you mean by MS Ajax? Are you making ana AJAX request and want the string response to be in that format? – jbkkd Aug 05 '13 at 08:58
  • No. need to format based on MicrosoftAjax.js file. – RGR Aug 05 '13 at 09:07

1 Answers1

2

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() || "" })
georg
  • 211,518
  • 52
  • 313
  • 390
  • Note: [browser compatibility](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind#Browser_compatibility) for `.bind()`. – Jonathan Lonowski Aug 05 '13 at 09:31
  • Thanks @thg435!. But if a digit is lesser than 10 digits , i am getting 'undefined' at the end. – RGR Aug 05 '13 at 10:28