Question

In my database, I store phone numbers like this "7279884545". One solid string with no spaces.

In my class that stores the phone number info, I have a function that will add the correct punctuation.

public static String beautifyPhoneNumber(String number, String extension)
        {
            String beautifulNumber = "";
            if (!String.IsNullOrEmpty(number))
            {
                beautifulNumber = "(" + number.Substring(0, 3) + ") " +
                                         number.Substring(3, 3) + "-" +
                                         number.Substring(6, 4);
            }
                if (!String.IsNullOrEmpty(extension))
                {
                    beautifulNumber += " x" + extension;
                }

            return beautifulNumber;
        }

And here is how I have the variable in the class itself.

private string _PhonePrimary;
[DisplayName("Phone Primary")]
public string PhonePrimary
{
    get
    {
        if(this._PhonePrimary != null)
        {
             this._PhonePrimary = beautifyPhoneNumber(this._PhonePrimary, this.Extension);
        }
        return this._PhonePrimary;
    }
    set
    {
        this._PhonePrimary = value;
    }
}

This works fine most of the time. The numbers are outputted to the screen in a "(727) 988-4545" or "(727) 988-4545 x12" if there is an extension for that record in the database.

The problem comes when I do a HttpPost request. The model information that is inside of the post request looks like this.

_PhonePrimary = "(727) 988-4545"

PhonePrimary = "(((7) 2) -7) -"

Was it helpful?

Solution

As noted, it looks like you're calling beautifyPhoneNumber on a number you've already beautified.

Here's an implementation using regular expressions that should get you started:

public static String BeautifyPhoneNumber(string numberToBeautify)
    {
        //The below gives us capture groups for each 
        //individual piece of the number.
        var regularExpression = new Regex(@"(\d{3})(\d{3})(\d{4})(x\d*)?");
        //This matches a number that's already been beautified, 
        //so we can guard against beautifying twice.
        var alreadyBeautifulExpression = new Regex(@"(\(\d{3}\)) (\d{3})-(\d{4}) ?(x\d*)?");
        var beautifulNumber = string.Empty;
        var separator = "-";
        var space = " ";
        //This prevents us from accidentally beautifying 
        //something more than once
        //You could also guard against this in your getter using a
        //IsBeautified extension, using the alreadyBeautifulExpression above
        if (alreadyBeautifulExpression.IsMatch(numberToBeautify))
        {
            return numberToBeautify;
        }

        //Trying to protect against invalid input... May be insufficient,
        //Or unnecessary
        if (string.IsNullOrEmpty(numberToBeautify) 
            || regularExpression.Matches(numberToBeautify).Count <= 0)
        {
            return beautifulNumber;
        }

        GroupCollection groups = regularExpression.Matches(
                                                numberToBeautify)[0].Groups;

        //More protection against invalid input
        if (groups.Count < 3)
        {
            return beautifulNumber;
        }

        //Given "7689131234",
        beautifulNumber += "(" + groups[1] + ")" + space; //gives us "(768) "
        beautifulNumber += groups[2] + separator; //gives us "(768) 913-"
        beautifulNumber += groups[3]; //gives us "(768) 913-1234"
        //If we have an extension, we add it.
        if (groups[4] != null)
        {
            beautifulNumber += space + groups[4];
        }

        return beautifulNumber;
    }

Given inputs of: 7279884545 7279884545x12 (727) 988-4545

This returns: (727) 988-4545 (727) 988-4545 x12 (727) 988-4545

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top