In the following code I need to modify it so that the btnSave control's click event procedure determines whether the user has entered the phone number in the required format: three digits, a hyphen, three digites, a hyphen, and four digits. If not a message should display saying the format is incorrect. The text box (txtPhone) is used to enter the phone number, the numbers are then added to a text file (phoneNumbers.txt). In the original there are to labe boxes for output, one for the numbers as they appear on the text file(111222333) called lblFileContents, and another after being correctly formatted through code(111-222-3333) called lblFormattedNumbers. Obviously both label boxes will display the same numbers once the code is modified to accept only the proper format(111-222-3333). Here is the original code:

Public Class frmMain

Private Sub btnExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExit.Click
    Me.Close()
End Sub

Private Sub txtPhone_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtPhone.KeyPress
    ' allows the text box to accept numbers, the hyphen, and the Backspace key

    If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso e.KeyChar <> "-" AndAlso e.KeyChar <> ControlChars.Back Then
        e.Handled = True
    End If
End Sub

Private Sub txtPhone_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtPhone.TextChanged
    lblFileContents.Text = String.Empty
    lblFormattedNumbers.Text = String.Empty
End Sub

Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
    ' saves the phone number to a sequential access file

    ' declare variables
    Dim strPhone As String
    Dim outFile As IO.StreamWriter

    ' remove the hyphens
    strPhone = txtPhone.Text.Replace("-", String.Empty)
    ' verify the length
    If strPhone.Length = 10 Then
        outFile = IO.File.AppendText("phoneNumbers.txt")
        outFile.WriteLine(strPhone)
        outFile.Close()
    Else
        MessageBox.Show("Invalid phone number", 
                        "Phone Numbers",
                        MessageBoxButtons.OK, 
                        MessageBoxIcon.Information)
    End If

    ' clear the txtPhone control, then set the focus
    txtPhone.Text = String.Empty
    txtPhone.Focus()
End Sub

Private Sub btnDisplay_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
    ' displays the phone numbers contained in the file
    ' also displays the phone numbers after inserting hyphens 

    ' declare variables
    Dim inFile As IO.StreamReader
    Dim strPhone As String

    ' clear previous phone numbers from the labels
    lblFileContents.Text = String.Empty
    lblFormattedNumbers.Text = String.Empty

    ' determine whether the file exists
    If IO.File.Exists("phoneNumbers.txt") = True Then
        ' open the file for input
        inFile = IO.File.OpenText("phoneNumbers.txt")
        ' process loop until end of the file
        Do Until inFile.Peek = -1
            ' read a phone number, then display the number
            strPhone = inFile.ReadLine
            lblFileContents.Text = lblFileContents.Text &
                strPhone & ControlChars.NewLine
            ' display the phone number with hyphens
            strPhone = strPhone.Insert(3, "-")
            strPhone = strPhone.Insert(7, "-")
            lblFormattedNumbers.Text =
                lblFormattedNumbers.Text &
                strPhone & ControlChars.NewLine


        Loop
        ' close the file
        inFile.Close()
    Else
        MessageBox.Show("Can't find the phoneNumbers.txt file",
                        "Phone Numbers", MessageBoxButtons.OK,
                        MessageBoxIcon.Information)
    End If
End Sub

End Class

Any help is greatly appreciated.

The easiest way to check is to use a regular expression. Here is an example for your particular requirements:

Imports System.Text.RegularExpressions

Public Class Form1

    Private Sub btnTest_Click(sender As System.Object, e As System.EventArgs) Handles btnTest.Click

        If Regex.IsMatch(txtTestStr.Text, "^[2-9]\d{2}-\d{3}-\d{4}$") Then
            txtResult.Text = "Match"
        Else
            txtResult.Text = "No Match"
        End If

    End Sub

The above sub compares the text in the TextBox, txtTestStr.Text, with the regular expression "^[2-9]\d{2}-\d{3}-\d{4}$" and displays the result in another text box. The regular expression can be broken down as follows

    ^       the start of the string
    [2-9]   any digit in the range 2 to 9
    \d{2}   exactly two other digits
    -       a dash
    \d{3}   exactly three digits
    -       a dash
    \d{4}   exactly four digits
    $       the end of the string

You can find many other useful patterns here

commented: nice +1

"^[2-9]\d{2}-\d{3}-\d{4}$"

Once again you have been very helpful, thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.