Is there a way I can format for example: 0000000000 into (000)000-0000? I'm returning a listbox which holds a collection of phone number which arent formated yet. What I would like is format it. This is what I have in the View:
<%= Html.ListBox("phoneList")%>
and from the controller:
ViewData["phoneList"] = new SelectList(phoneList);
Edit
$('#phoneList').each(function() {
var phoneNumber = $(this).text();
var formatPhoneNumber = phoneNumber.replace(/(\d{3})(\d{3})(\d{4})/, '($1)$2-$3');
alert(formatPhoneNumber);
});
how would I assign this back to show in the ListBox?
Is there a way I can format for example: 0000000000 into (000)000-0000? I'm returning a listbox which holds a collection of phone number which arent formated yet. What I would like is format it. This is what I have in the View:
<%= Html.ListBox("phoneList")%>
and from the controller:
ViewData["phoneList"] = new SelectList(phoneList);
Edit
$('#phoneList').each(function() {
var phoneNumber = $(this).text();
var formatPhoneNumber = phoneNumber.replace(/(\d{3})(\d{3})(\d{4})/, '($1)$2-$3');
alert(formatPhoneNumber);
});
how would I assign this back to show in the ListBox?
Share Improve this question edited Aug 27, 2010 at 1:28 hersh asked Aug 26, 2010 at 21:01 hershhersh 1,1832 gold badges13 silver badges26 bronze badges 3- 1 This will only work for US style phone numbers. UK & other European countries format their numbers differently. – ChrisF ♦ Commented Aug 26, 2010 at 21:20
-
hersh - Just saw your ment before it disappeared, so I assume you figured it out. I don't know exactly what type of element you have, but I imagine if you got the original value with
$(this).text()
, you should be able to assign the new value the same way.$(this).text(formatPhoneNumber)
– user113716 Commented Aug 27, 2010 at 2:22 - patrick - yes, I figured it out and thanks for getting back at me. – hersh Commented Aug 27, 2010 at 17:52
2 Answers
Reset to default 6If you're saying that you want to do it on the client side, then given the phone number in a variable, you could do this:
http://jsfiddle/HZbXv/
var str = "0000000000";
var res = '(' + str.substr(0,3) + ')' + str.substr(3,3) + '-' + str.substr(6);
alert(res);
or this:
http://jsfiddle/HZbXv/1/
var str = "0000000000";
var res = str.replace(/(\d\d\d)(\d\d\d)(\d\d\d\d)/, '($1)$2-$3');
alert(res);
EDIT:
As noted by @Nick Craver The second version can be shortened up with:
var res = str.replace(/(\d{3})(\d{3})(\d{4})/, '($1)$2-$3');
As simple as this. Can use a simple if statement if you are going to deal with multiple numbers.
YourVariable = YourVariable.replace(/(\d{1})(\d{3})(\d{3})(\d{4})/, "+$1 ($2)-$3-$4");
Displays: +1 (800)-555-5555
YourVariable = YourVariable.replace(/(\d{3})(\d{3})(\d{4})/, "($1)-$2-$3");
Displays (555)-555-5555
If statement (my own code):
if (form.FaxNumber.length = 11){
form.FaxNumber.value = form.FaxNumber.value.replace(/(\d{1})(\d{3})(\d{3})(\d{4})/, "+$1 ($2)-$3-$4");
}
if (form.FaxNumber.length = 10){
form.FaxNumber.value = form.FaxNumber.value.replace(/(\d{3})(\d{3})(\d{4})/, "($1)-$2-$3");
}