locked
VB.NET Format Phone Number

    Question

  • I'm trying to format a phone number in VB.NET 2008 in the code. A user can type in this number for example: (203) 555-1212

    The format I would like the phone numbers to appear is this:
    203.555.1212

    I haven't been able to find the correct format.

    I tried this:

     

    Dim x As String = "(222) 333-4444"

     

    Dim joe As String = String.Format("{0:###.###.####}", x)

    This doesn't do anything to the variable. I've tried to set my variable x to '2223334444', but the format still does not work. Any ideas?

    Monday, November 23, 2009 5:06 PM

Answers

  • I'm not sure how to use RegEx. And then it seems I would have to rebuild my string anyways. So I wrote code that strips out the parens and dashes, and then rebuilt the string from there. Code is below. If anyone has an easier way of doing what I need vs. all of the code I came up with, please let me know.

     

    Dim first, middle, last As String

     

    Dim phonenumber As String = "(555)122-3333"

     

    Dim finalnumber As String

     

    'Strip out all '(', ')' and '-'

    phonenumber = Replace(phonenumber,

    "(", "")

    phonenumber = Replace(phonenumber,

    ") ", "")

    phonenumber = Replace(phonenumber,

    ")", "")

    phonenumber = Replace(phonenumber,

    "-", "")

     

    If Len(phonenumber) = 10 Then

     

    'Get the 1st 3 digits

    first = Mid(phonenumber, 1, 3)

     

    'Add a '.'

    finalnumber = first &

    "."

     

    'Get the next 3 digits

    middle = Mid(phonenumber, 4, 3)

     

    'Add the middle 3 digits to our phone

    finalnumber = finalnumber & middle &

    "."

     

    'Get the last 4 digits

    last = Mid(phonenumber, 7, 4)

    finalnumber = finalnumber & last

    MsgBox(finalnumber)

     

    End If

     

     






     

     

    • Marked as answer by JMANCI Wednesday, November 25, 2009 3:14 PM
    Wednesday, November 25, 2009 3:14 PM

All replies

  • I don't believe you can use String.Format in this manner.  The formatting symbols "#" and "." are used to format numbers.  Even if you remove the existing format symbols "(", ")" and "-" and try to format just the numbers, String.Format with your specified format won't work because a number can only have one decimal.

    All the phone formatting code i've found uses regex (to remove existing format chars) and substrings (to break a phonenumber into peices) and then reconstitutes the number with the desired format symbols (either string concat or string.format).

    Check this for sample using substring & string.format  
    http://aspalliance.com/718_Code_Snippet_Format_Phone_Numbers_using_StringFormat
    Monday, November 23, 2009 7:59 PM

  • This type is untested.
    The regex string I show will ignore the parentheses your example.


    Public Structure PhoneNumber
        Public Number As String

        Public Sub New(ByVal number As String)
            If VerifyFormat(number) Then
                Me.Number = number
            Else
                Throw New InvalidOperationException("Invalid Format")
            End If
        End Sub

        Private Function VerifyFormat(ByVal number As String)
            Dim regex As Regex = New Regex("^\d{3}-\d{3}-\d{4}$")
            Return regex.IsMatch(number) 'use regex to verify your desired format
        End Function

        Public Overrides Function ToString() As String
            Return Number
        End Function

    End Structure



    Rudy  =8^D

    Mark the best replies as answers. "Fooling computers since 1971."
    Monday, November 23, 2009 8:49 PM
  • I'm not sure how to use RegEx. And then it seems I would have to rebuild my string anyways. So I wrote code that strips out the parens and dashes, and then rebuilt the string from there. Code is below. If anyone has an easier way of doing what I need vs. all of the code I came up with, please let me know.

     

    Dim first, middle, last As String

     

    Dim phonenumber As String = "(555)122-3333"

     

    Dim finalnumber As String

     

    'Strip out all '(', ')' and '-'

    phonenumber = Replace(phonenumber,

    "(", "")

    phonenumber = Replace(phonenumber,

    ") ", "")

    phonenumber = Replace(phonenumber,

    ")", "")

    phonenumber = Replace(phonenumber,

    "-", "")

     

    If Len(phonenumber) = 10 Then

     

    'Get the 1st 3 digits

    first = Mid(phonenumber, 1, 3)

     

    'Add a '.'

    finalnumber = first &

    "."

     

    'Get the next 3 digits

    middle = Mid(phonenumber, 4, 3)

     

    'Add the middle 3 digits to our phone

    finalnumber = finalnumber & middle &

    "."

     

    'Get the last 4 digits

    last = Mid(phonenumber, 7, 4)

    finalnumber = finalnumber & last

    MsgBox(finalnumber)

     

    End If

     

     






     

     

    • Marked as answer by JMANCI Wednesday, November 25, 2009 3:14 PM
    Wednesday, November 25, 2009 3:14 PM
  • Check this thread for various Regex strings for different phone number formats.

    Telephone Number Input Validation



    Mark the best replies as answers. "Fooling computers since 1971."
    Wednesday, November 25, 2009 3:28 PM