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
236 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
Independent Software Vendors: 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
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…

733 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