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
233 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
  • 3
  • 2
  • 2
  • +3
12 Comments
 
LVL 85

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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
DO Loop not working 4 72
Pull multiple cvs files into one access table 28 64
Vb.net 2008 2 56
how can i unlock a picture file that was loaded in a picturebox in visual c#? 4 84
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…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

911 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now