Format string MVC Razor phone number indiscriminately to decimal

I am trying to format phone numbers that loop in my ModelModel view as follows: (###) ###-#### without the need to parse the string to decimal.

This method gives the error Formatting is specified, but argument is not IFormattable :

 @foreach (var client in Model.Clients) { <td>@String.Format("{0:(###) ###-####}", client.TelephoneNumber)</td> } 

So, I will need to parse the decimal number:

 @foreach (var client in Model.Clients) { <td>@String.Format("{0:(###) ###-####}", Decimal.Parse(client.TelephoneNumber))</td> } 

But there is no guarantee that the numbers looped in them will be just numbers, so most likely at least one parsing attempt will fail.

Is there any way to achieve this formatting without having to parse in decimal format?

+5
source share
3 answers

If your TelephoneNumber is a string , you can always use substrings to format the number. It is not so clean, but you do not need any separate libraries and convert to decimal :

  @String.Format("({0}) {1}-{2}" , client.TelephoneNumber.Substring(0, 3) , client.TelephoneNumber.Substring(3, 3) , client.TelephoneNumber.Substring(6, client.TelephoneNumber.Length - 6)) 
+2
source

Try this, you can put it in a static function somewhere for reuse.

 var match = Regex.Match("1231231234", @"(\d{3})(\d{3})(\d{4})"); Console.WriteLine( "(" + match.Groups[1] + ") " + match.Groups[2] + "-" + match.Groups[3]); 
0
source

Or even simpler if you are dealing only with 10-digit phone numbers:

 Regex.Replace(phoneNum, @"(\d{3})(\d{3})(\d{4})", "($1) $2-$3"); 

Read-only

If you are only interested in displaying the phone number, you can make it an HTML helper as follows:

 public static MvcHtmlString FormatPhoneNum(this HtmlHelper helper, string phoneNum) { //You could strip non-digits here to make it more robust if (String.IsNullOrEmpty(phoneNum)) return phoneNum; return new MvcHtmlString(Regex.Replace(phoneNum, @"(\d{3})(\d{3})(\d{4})", "($1) $2-$3")); //US Phone Number } 

And then use like this:

 @foreach (var client in Model.Clients) { <td>@Html.FormatPhoneNumber(client.TelephoneNumber)</td> } 

Editing

If you also need to edit the phone number and want to display it in the text box of the editor, you can create a shell property for your view model in order to convert the phone number:

 public class Client { public string TelephoneNumber {get; set;} //Require 10 digits, each surrounded by any non-digit characters (will strip all non-digits) [RegularExpression(@"(\D*\d\D*){10}", ErrorMessage = "Please enter a 10 digit phone number")] public string FormattedPhoneNum { get { MyHelpers.FormatPhoneNumber(TelephoneNumber); } set { TelephoneNumber = MyHelpers.StripPhoneNumber(value); } } } public class MyHelpers { public static StripPhoneNumber(string phone) { if (phone == null) return phone; else return _nonDigits.Replace(phone, String.Empty); } public static string FormatPhoneNumber(string phoneNum) { phoneNum = StripPhoneNumber(phoneNum); if (String.IsNullOrEmpty(phoneNum)) return phoneNum; return Regex.Replace(phoneNum, @"(\d{3})(\d{3})(\d{4})", "($1) $2-$3"); //US Phone Number } } 

Note the RegularExpressionAttribute attribute in the property. This requires a very soft position when entering the user. It is executed until the user enters at least 10 digits into the text field, regardless of any other characters entered. You may need to make it more restrictive for your own purposes.

0
source

Source: https://habr.com/ru/post/1232058/


All Articles