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
232 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
Comment Utility
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
Comment Utility
You are right I am working in it
0
 
LVL 9

Expert Comment

by:dancebert
Comment Utility
>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
Comment Utility
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
Comment Utility
Lay off!  I DO not need any help. Thanks
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 4

Expert Comment

by:brother7
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
PAQed - no points refunded (of 250)

PashaMod
Community Support Moderator
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
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…

771 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

12 Experts available now in Live!

Get 1:1 Help Now