Need help on Isnumeric function in VB.NET

Dear experts team:

My name is Larry Le and I am working on the project, which asks to verify the phone numbers when users enter into textbox.  Below is more info about the project

=======================================

Design and develop a program that validates the value a user enters into a TextBox control to ensure that the entry is a valid telephone number. 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

=======================================

Here is my codes for the program:


#End Region


    Private Sub cmdVerify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdVerify.Click
        Dim Num As String
        Num = "12"
        If IsNumeric(Num) = True Then
            txtTextBox1.Text = IsNumeric(Num)
            txtTextBox1.Text = txtTextBox1.Text
            MsgBox("Your number below is valided")

        Else
            MsgBox("Your number below is invalid")

        End If
    End Sub

    Private Sub cmdReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdReset.Click
        txtTextBox1.Text = "999-999-9999"
        MsgBox("Please Enter Next Number")
    End Sub

    Private Sub cmdQuit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdQuit.Click
        MsgBox("Please come back again")
        End
    End Sub
End Class

========================================

Based on the codes above, it lets me key in anything in textbox. However, if possible, Can you help me with the following condidion, I like

1.    Users enter numbers into textbox with two dashes in the appropriate spaces and it breaks the string into three     substrings, For example, 408-345-2323. If users enter right format as 408-345-2323, then message box     display "Number Validaded". If users enter wrong format (without dashes in between) as 4083452323, then     message box display "Number inValid because missing two dashes".

2.    Users can enter combination text and numbers, however, as soon as users click Verify botton, message     displays "This is not numbers".


Please advise.

Thanks in advance

Larry
larryleAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cookreCommented:
Given that the format MUST be ###-###-####, I'd do (Assuming typedstr is the string from the text box):

if len(typedstr)<>12 then it's bad
if not IsNumeric(substr(typedstr,1,3)) then it's bad
if substr(typedstr,4,1)<>"-" then it's bad
if not IsNumeric(substr(typedstr,5,3)) then it's bad
if substr(typedstr,8,1)<>"-" then it's bad
if not IsNumeric(substr(typedstr,9,4)) then it's bad
... if it gets here, it's good


0
larryleAuthor Commented:
Do I need to declar as

Dim typedstr as string
typedstr = txtTextBox1.Text

if len(typedstr)<>12 then it's bad
if not IsNumeric(substr(typedstr,1,3)) then it's bad
if substr(typedstr,4,1)<>"-" then it's bad
if not IsNumeric(substr(typedstr,5,3)) then it's bad
if substr(typedstr,8,1)<>"-" then it's bad
if not IsNumeric(substr(typedstr,9,4)) then it's bad
... if it gets here, it's good
0
larryleAuthor Commented:
In what part of my codes above, do I need to plug in your codes?  Please advise.

URGENT Please

Larry
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

cookreCommented:
I'd put it in the Leave event for the text box.
0
larryleAuthor Commented:
Sorry I am kind slow.  If you look at my codes above, you will not see the Leave event for the text box at all.  

So do you want me to create other event?

Larry
0
cookreCommented:
Presuming cmdVerify is a button that's used to validate the text box, you can put the validation code there.

If you had a lot of text boxes to validate, you wouldn't want a seperate button for each, nor would it typically be good form to validate them all at once.  The Leave event is a way to validate a box when focus passes from it, therby giving the user immediate results if something bad was typed in.

It's late where I am now, so I have to go.

If you have any more problems, let's hope there's someone else on who can help tonight.
0
larryleAuthor Commented:
cookre:

Thanks for all your help on this matter as always.  Have a good night.

Larry
0
larryleAuthor Commented:

Can anyone in the team support me and help to elaborate on this issue because I still do not get the answere?

Thanks

Larry
0
cookreCommented:
How'd it go?
0
larryleAuthor Commented:
It did not go better since last night where you left off.  
0
cookreCommented:
OK.
From your original post I see you have event handler cmdVerify_Click()
From this I presume you have a button that is clicked to validate the text field.
If that's the case, just put the validation code in that handler.


The code I posted above was just an outline.  Since I don't have VB.NET where I am today, this is just an initial guess at what it would look like.  I've got it at home, so, if you don't get this thing  working by tonight, well, I guess it's about time I did some VB.NET

Note: For VB.NET substr()'s, I don't know if character positions start with 0 or 1.


' Make sure length is correct
if txttextbox1.text.length<>12 then
   messagebox.show("Input text is not 12 characters long")
   return
   end if

' Make sure th dashes are in the right spots
if txttextbox1.text.substr(4,1)<>"-" then
   messagebox.show("- missing in position 4")
   return
   end if
if txttextbox1.text.substr(8,1)<>"-" then
   messagebox.show("- missing in position 8")
   return
   end if

' Make sure the rest are numeric
if not txttextbox1.text.substr(1,3).isnumeric then
   messagebox.show("Invalid Area Code")
   return
   end if
if not txttextbox1.text.substr(5,3).isnumeric then
   messagebox.show("Invalid Exchange")
   return
   end if
if not txttextbox1.text.substr(9,4).isnumeric then
   messagebox.show("Invalid Extention")
   return
   end if

' Looks like it's OK
messagbox.show("Yum, that was good")
return
0
larryleAuthor Commented:
cookre,

This program still not work at all.  You have total six IF (condition) statements.  

1. The first condition statement, it does not matter how many digits I enter in, it just    display the same message "Input Text is not 12 characters long"

2.  The second and third condition statements, it does the same, just display exact the same as first statement.  I try enter 1234567899 and 123-456-7899 into textbox.  For both, message display "Input Text is not 12 characters long".  Note, I did key in the exact 12 characters long.

3.  The fourth, fifth, and sixth, VB.NET complains that "Isnumeric" is not a member of "string".


So, I am thinking of "Select Case"  statement or "For...Next" loop or string method.

Any idea.

Larry
0
cookreCommented:
Well, I know how VB.NET's substring works now.

I called my text box 'tbPhone' and the vaidation button 'cbValidate'.


Private Sub cbValidate_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
                                        Handles cbValidate.Click

' Make sure it's the right length
If tbPhone.Text.Length() <> 12 Then
   MessageBox.Show("Phone number not 12 characters")
   Return
   End If

' Make sure there are dashes in the right spots
If tbPhone.Text.Substring(3, 1) <> "-" Then
   MessageBox.Show("No dash in 4th position")
   Return
   End If
If tbPhone.Text.Substring(7, 1) <> "-" Then
   MessageBox.Show("No dash in 8th position")
   Return
   End If

' Make sure the Area Code is numeric
If Not IsNumeric(tbPhone.Text.Substring(0, 3)) Then
   MessageBox.Show("Area Code is not numeric")
   Return
   End If

' Make sure the Exchange is numeric
If Not IsNumeric(tbPhone.Text.Substring(4, 3)) Then
   MessageBox.Show("Exchange is not numeric")
   Return
   End If

' Make sure the Extention is numeric
If Not IsNumeric(tbPhone.Text.Substring(8, 4)) Then
   MessageBox.Show("Extention is not numeric")
   Return
   End If

MessageBox.Show("Yummie")
End Sub
End Class

This works fine except for one thing.  Since the three numeric fields are tested with an IsNumeric(), entries like '.05', '+23', i.e., legitimate numbers with leading signs and/or decimal oints will pass the IsNumeric() test.

Can you think of a way to fix that?

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
larryleAuthor Commented:
What about if users enter into textbox as follow

408-222-3333, which is correct.  But as soon as user press "Verify" button and a pop up message displays as "this is a valid number, and also it shows as 4082223333, instead of 408-222-3333.

Do you have any idea how to do that?

Thanks

Larry
0
cookreCommented:
If you want to create 408222333 from 408-222-333, just extract out the three pieces and stick them together.

You've seen how to extract out substrings.

To concatenate strings, use the '+' operator between strings.  For example, if string s1 has "abc" and string s2 has "def", then setting string s3

s3=s2+s1

will yield "defabc".

Similarly, s3.substring(0,4)+s1.substring(1,2) will yield "defabc".
0
larryleAuthor Commented:


What about if users key in number as follow

408-222-3333, which is correct format.  But, when user hit "Verify" button to verify it should display "This is number is valid"

Please help

Larry
0
cookreCommented:
Look back at the most recent code I posted.
When does it display "Yummie"?
0
larryleAuthor Commented:

Sorry I can not figure it out.

Now if we type in 408-222-3333 and press Verify button; it does nothing.  Please help

Thanks

Larry
0
cookreCommented:
If it doesn't give you "Yummie", something was typed in wrong.  Post your code and let's see.
0
larryleAuthor Commented:

#End Region


    Private Sub cmdVerify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdVerify.Click
        Dim MyNumber As String
        If txtTextBox1.Text.Length() < 12 Then
            MessageBox.Show("Phone number is not 12 characters long")
            Return
        End If

        If txtTextBox1.Text.Substring(3, 1) <> "-" Then
            MessageBox.Show(" - Missing in 4th position")
            Return
        End If

        If txtTextBox1.Text.Substring(7, 1) <> "-" Then
            MessageBox.Show(" - Missing in 8th position")
            Return
        End If

        MyNumber = txtTextBox1.Text.Substring(1, 3)
        If Not IsNumeric(MyNumber) Then
            MessageBox.Show("Area Code is not numeric")
            Return
            MyNumber = ""
        End If

        MyNumber = txtTextBox1.Text.Substring(5, 3)
        If Not IsNumeric(MyNumber) Then
            MessageBox.Show("Exchange is not numeric")
            Return
            MyNumber = ""
        End If

        If Not IsNumeric(txtTextBox1.Text.Substring(8, 4)) Then
            MessageBox.Show("Extention is not numeric")
            Return
        End If

    End Sub

    Private Sub cmdReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdReset.Click
        txtTextBox1.Text = "999-999-9999"
        MsgBox("Please Enter Next Number")
        txtTextBox1.Focus()
    End Sub

    Private Sub cmdQuit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdQuit.Click
        MsgBox("Please come back again")
        End
    End Sub
End Class
0
cookreCommented:
Two things:

  If txtTextBox1.Text.Length() < 12 Then
should be
  If txtTextBox1.Text.Length() <> 12 Then
otherwise a string longer than 12 wouldn't be caught.

You left out the display just before the End Sub of cmdVerify_Click.
Ask yourself which strings pass all of the filters to drop past all the IFs in cmdVerify_Click.
Yup, good ones.
0
larryleAuthor Commented:
Below are two lines of codes that I added and placed it before End Sub click event:


 MyNumber = Replace(txtTextBox1.Text, "-", "")
        MessageBox.Show("This is a valid number:  " & MyNumber, Me.Text,  MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

Larry
0
larryleAuthor Commented:
Thank you for following, helping, and supporting throughout this matter. I am very pleased to having you to work with me on this matter and without you I do not think that I am able to achieve this task on time.

I also like to thank for your patient and openning up for communication.

Again great help and support. YOU SHOULD HAVE AN A+++++ FOR THIS ONE.

Thanks again as always. I really appreciate it.

Have a nice day

Larry

P.S. This program is working now and it works great.
0
cookreCommented:
Commendations on coming up with Replace() - that's clearer that extracting the individual pieces.

Not bad for a first thread - don't forget you have to grade and close the question.
0
khubpkCommented:
Hi
I give U a very good Option In which it is not posible user enter Rong telle Phone number
In Key press event on the text box
write code

if textbox1.text.length()= 3 OR textbox1.text.length()= 6 then
         textbox1.text +="-"
end if
 another check on key press Ascii encoder
create encoder object and write
 if enobj.ascii.getcode >=48 and enobj.ascii.getcode<=57 then
'do nothing
else
enobj.ascii =0
end if
 
0
larryleAuthor Commented:

Great.  Thanks and I really appreciate it.

Larry
0
Snapa02Commented:
Okay- only thing with this- I had to do this assignment too- and found this to be very helpful, but in the event that the user should enter a non-numeric symbol along with numbers, separated by dashes- it comes out saying that it is a valid phone number.  

For example- 12.-345-234j

Does anyone know how to fix this by tweaking the above mentioned code?  

Thank you!

Snapa
0
jtbaloghCommented:
Dont piggy back this old question with what you need. That is not how the forum works. Ask your own new question and refer back to this if necessary. I only noticed you by accident after coming from,
    http://www.experts-exchange.com/Programming/Q_20861704.html
Hope that helps.
Joseph
0
Snapa02Commented:
Kind of harsh- but thanks anyway.  I am new here- and did not know.  Noted.

Snapa
0
jtbaloghCommented:
I could have rephrased it nicer. Sorry.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.