0

I want to display the phone number like this (999) 999-9999 I thought the css class would do this but it doesn't

<td>@Html.Encode(Model.PhoneModel.PhoneNumber)</td>.

I tried this, but it doesn't work

@String.Format("{0:(###) ###-####}",Model.PhoneModel.PhoneNumber.ToString())
Ronald McDonald
  • 2,843
  • 11
  • 49
  • 67

3 Answers3

1

I got this to work, but I don't like the fact that it's regrex.

@Regex.Replace(Model.PhoneModel.PhoneNumber, @"(\d{3})(\d{3})(\d{4})", "($1) $2-$3")
Rarepuppers
  • 723
  • 1
  • 10
  • 21
Ronald McDonald
  • 2,843
  • 11
  • 49
  • 67
0

You may want to look at changing how you are outputting the values. Consider this article from Phil Haack.

http://haacked.com/archive/2010/04/28/replacing-html-encode.aspx

EDIT: To address your edit of formatting as a phone number reference this thread: How to format a string as a telephone number in C#

String.Format("{0:(###) ###-####}", 8005551212);
Community
  • 1
  • 1
Shawn
  • 1,871
  • 2
  • 21
  • 36
0

There's not much point in spending too much effort on formatting phone numbers, when others have done all the work for you. Check out http://code.google.com/p/libphonenumber/ for a library which will format numbers (and convert them back to a standard format) for you. It's also available on nuget as libphonenumber-csharp.

Richard
  • 29,854
  • 11
  • 77
  • 120