Solved

I need a program that validates the value a user enters into a TextBox control to ensure that the entry is a valid telephone number.

Posted on 2003-10-27
12
238 Views
Last Modified: 2010-05-03
The application should accept a maximum of 12 characters. 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.
0
Comment
Question by:haulottek
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +3
12 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 9631376
Hmmm....we can't do your homework here on EE.  We can help you with a specific problem in your code though.

So, get to work!

Cheers
0
 

Author Comment

by:haulottek
ID: 9631454
You are right I am working in it
0
 
LVL 9

Expert Comment

by:dancebert
ID: 9631461
>So, get to work!
Or drop the class.  Being either unable or unmotivated to do your programming homework should be a huge red flag that you don't have what it takes to build systems for a living.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:pg_india
ID: 9631569
what u can do is to make a masked box with format as ###-###-####

set maxlength to 12 and then in key press check for the ascii value should be between 48 to 57..

also if error is there then u cnd do text1.text = "" 
text1.setfocus
or whatever u want to do
if any problem do report...


0
 

Author Comment

by:haulottek
ID: 9631683
Lay off!  I DO not need any help. Thanks
0
 
LVL 4

Expert Comment

by:brother7
ID: 9631713
Tried and tested by me :)

Create a form with textbox txtInput and button cmdValidate.

--- CODE START ---

Private Sub cmdValidate_Click()

Dim areaCode, prefix, suffix As String
Dim isValid As Boolean

' assume a valid telephone number, try to invalidate it
isValid = True

' get the strings where numbers should be
areaCode = Left$(txtInput.Text, 3)
prefix = Mid$(txtInput.Text, 5, 3)
suffix = Right$(txtInput.Text, 4)

' check for valid length
If Len(txtInput.Text) <> 12 Then isValid = False

' check for numeric portions of telephone number
If Not IsNumeric(areaCode) Then isValid = False
If Not IsNumeric(prefix) Then isValid = False
If Not IsNumeric(suffix) Then isValid = False

' check that hyphens delimit the telephone number
If Mid$(txtInput.Text, 4, 1) <> "-" Or Mid$(txtInput.Text, 8, 1) <> "-" Then isValid = False

' tricky part... check that first and last characters of numeric parts aren't signs!
If Left$(areaCode, 1) = "+" Or Left$(areaCode, 1) = "-" Then isValid = False
If Right$(areaCode, 1) = "+" Or Right$(areaCode, 1) = "-" Then isValid = False
If Left$(prefix, 1) = "-" Or Left$(prefix, 1) = "-" Then isValid = False
If Right$(prefix, 1) = "-" Or Right$(prefix, 1) = "-" Then isValid = False
If Left$(suffix, 1) = "-" Or Left$(suffix, 1) = "-" Then isValid = False
If Right$(suffix, 1) = "-" Or Right$(suffix, 1) = "-" Then isValid = False


If isValid Then
    returnValue = MsgBox("This is a valid telephone number." & vbCrLf & vbCrLf & _
        "Area Code = " & areaCode & vbCrLf & _
        "Prefix = " & prefix & vbCrLf & _
        "Suffix = " & suffix, vbOKOnly, "Validation Result")
Else
    returnValue = MsgBox("This is NOT a valid telephone number.", vbOKOnly, "Validation Result")
End If
End Sub

--- CODE END ---
0
 
LVL 4

Expert Comment

by:brother7
ID: 9631792
New and improved version!
I forgot to give explanation why telephone number is invalid.  This version also correctly clears the textbox and sets the focus.

--- CODE START ---
Private Sub cmdValidate_Click()

Dim areaCode, prefix, suffix, errorMsg As String
Dim isValid As Boolean

' assume a valid telephone number, try to invalidate it
isValid = True

' get the strings where numbers should be
areaCode = Left$(txtInput.Text, 3)
prefix = Mid$(txtInput.Text, 5, 3)
suffix = Right$(txtInput.Text, 4)

' check for numeric portions of telephone number
If Not IsNumeric(areaCode) Then
    isValid = False
    errorMsg = "Area Code " & areaCode & " is not numeric."
End If
If Not IsNumeric(prefix) Then
    isValid = False
       errorMsg = "Prefix " & prefix & " is not numeric."
End If
If Not IsNumeric(suffix) Then
    isValid = False
    errorMsg = "Suffix " & suffix & " is not numeric."
End If

' tricky part... check that first and last characters of numeric parts aren't signs!
If Left$(areaCode, 1) = "+" Or Left$(areaCode, 1) = "-" Or _
    Right$(areaCode, 1) = "+" Or Right$(areaCode, 1) = "-" Or _
    Left$(prefix, 1) = "-" Or Left$(prefix, 1) = "-" Or _
    Right$(prefix, 1) = "-" Or Right$(prefix, 1) = "-" Or _
    Left$(suffix, 1) = "-" Or Left$(suffix, 1) = "-" Or _
    Right$(suffix, 1) = "-" Or Right$(suffix, 1) = "-" Then
        isValid = False
        errorMsg = "Why are you trying to trick me?" & vbCrLf & _
            "A + or - sign is hidden in the telephone number."
End If

' check that hyphens delimit the telephone number
If Mid$(txtInput.Text, 4, 1) <> "-" Or Mid$(txtInput.Text, 8, 1) <> "-" Then
    isValid = False
    errorMsg = "Hyphen delimiters are missing or misplaced."
End If

' check for valid length
If Len(txtInput.Text) <> 12 Then
    isValid = False
    errorMsg = "Input string is incorrect length (" & Len(txtInput.Text) & ")"
End If

' display message box
If isValid Then
    returnValue = MsgBox("This is a valid telephone number." & vbCrLf & vbCrLf & _
        "Area Code = " & areaCode & vbCrLf & _
        "Prefix = " & prefix & vbCrLf & _
        "Suffix = " & suffix, vbOKOnly, "Validation Result")
Else
    returnValue = MsgBox("This is NOT a valid telephone number." & vbCrLf & vbCrLf & _
        errorMsg, vbOKOnly, "Validation Result")
End If

txtInput.Text = ""
txtInput.SetFocus


End Sub

--- CODE END ---
0
 
LVL 3

Expert Comment

by:pg_india
ID: 9632078
But i feel my way is simpler and it saves from lot of coding...

comments plzzzz

add a component : masked textbox
AND THIS CODE...
Private Sub Command6_KeyPress(KeyAscii As Integer)
If KeyAscii < 48 Or KeyAscii > 57 Then
    KeyAscii = 0
End If
End Sub
0
 
LVL 4

Expert Comment

by:brother7
ID: 9632124
In the problem statement, it says "Use String class methods to solve the problem."  I'm thinking that his teacher wants him to get familiar with Left$, Mid$ and Right$.
0
 

Accepted Solution

by:
PashaMod earned 0 total points
ID: 10371860
PAQed - no points refunded (of 250)

PashaMod
Community Support Moderator
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question