I have written a function that will take a phone number and format it to (xxx)-xxx-xxxx. Everything works however I need it to only run if the number is 10 digits long and as long as there are no letters. What do I need to be able to throw a messagebox saying error if either the number is < or > than 10 digits And/Or if there are alpha characters in the textbox. Here is my Function thank again for any help.
Code:
Function FormatPhoneNumber(ByVal txtPhoneNumber As String) As String
Try
txtFormattedPhoneNumber.Text = String.Format("({0}) {1}-{2}", txtPhoneNumber.Substring(0, 3),
txtPhoneNumber.Substring(3, 3), txtPhoneNumber.Substring(6, 4))
Catch ex As Exception
MessageBox.Show("Error")
End Try
Return txtPhoneNumber
End Function