[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
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
Medium Priority
?
250 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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…
Suggested Courses

649 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