Validate a Phone Number

vargf

Member
Joined
Feb 15, 2006
Messages
13
Programming Experience
Beginner
I want to now how validate a Phone number where accept12 Characters only.When the user clicks a button, the program should determine if the entry is of the form 999-999-9999, where the character 9 represents any number.
If the entry is determined to be a telephone number, display an appropriate message, along with the separate substrings that compose the telephone number without the dashes. If the entry is not a telephone number, then display a message as to the reason why the entry is not a valid telephone number, clear the TextBox control and set focus to the TextBox control. Use String class methods to solve the problem.
A. Check that the user has entered two dashes in the appropriate spaces.
B. Break the string into 3 substrings, represented as 999,999, and 9999.

Logic
  1. <LI class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-list: l0 level2 lfo2; tab-stops: list 1.0in">I want to user feedback on the correctness of the input. Some are more user friendly than others. However, it may require a lot of coding to account for various situations. <LI class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-list: l0 level2 lfo2; tab-stops: list 1.0in">The simple way is the following:
    • <LI class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-list: l0 level3 lfo2; tab-stops: list 1.5in">First I want to check the length of the input string: it must be exactly 12. <LI class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-list: l0 level3 lfo2; tab-stops: list 1.5in">Then check for Substring(3, 1) and Substring(7, 1),i.e. the fourth and eighth character must be “-“ <LI class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-list: l0 level3 lfo2; tab-stops: list 1.5in">I want to use the Substring method to separate the input string into 3 parts <LI class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-list: l0 level3 lfo2; tab-stops: list 1.5in">I want to see if each part is only numeric characters, using IsNumeric and IndexOf.
    • If error, display error message box.
    • I want to use a loop if is possible,but I have to use the if-then-else statement
Can any body helpme on this project because I want to have skills in this
particular think
thaks Z
 
Typically a method you call to have one or more fields validated:
VB.NET:
Sub validateTB()
  Dim ex1 As String = "^[0-9]{3}-[0-9]{3}-[0-9]{4}$" 
  If RegularExpressions.Regex.Match(txtInput.text, ex1).Success = False Then
    ErrorProvider1.SetError(txtInput, "please provide a valid phone number 000-000-0000")
  Else
    ErrorProvider1.SetError(txtInput, "")
  End If
End Sub
http://www.regular-expressions.info

// EDIT: I just forgot to terminate to 12chars by using beginline '^' and endline '$'
// this can also be expressed like this ^(\d{3}-){2}\d{4}$ but the above is easier to read, no need to make this look complicated.
 
Last edited:
help with this method in validating a phone number

I start practicing with the telephone validation I got one with a sub procedure,but somthing I did wrong on blank or wrong format (999999999)field respond good with the Msg Invalid Phone Number,but when I put the correct format(999-999-9999) still the Msg "Invalid Phone Number" what did you think it is the problem? Can any body help me?
thanks
My Code
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim PhoneNumbervar As String
Dim InputText As String
'Do
InputText = Trim(numPhoneTextinput.Text)
CheckPhoneNum(PhoneNumbervar)
'Loop Until PhoneNumbervar = ""
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
numPhoneTextinput.Text = " "
End Sub
Sub CheckPhoneNum(ByVal PhoneNumber As String)
Dim Msg As String
Dim PhoneNumbervar As String
'PhoneNumbervar = Trim(numPhoneTextinput.Text)
If Len(PhoneNumbervar) > 12 Then
Msg = "invalid number" & PhoneNumber & "!"
ElseIf Mid(PhoneNumbervar, 4, 1) <> "-" And Mid(PhoneNumbervar, 8, 1) <> "-" Then
Msg = "invalid number" & PhoneNumber & "!"
Else
Msg = "number is in the correct format" & PhoneNumber & "!!!!"
End If
MsgBox(Msg, , "Telephone Number")
End Sub
End
Class
 
Back
Top