Solved

Need help on Isnumeric function in VB.NET

Posted on 2003-10-27
30
1,634 Views
Last Modified: 2010-05-18
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
0
Comment
Question by:larryle
  • 14
  • 11
  • 2
  • +2
30 Comments
 
LVL 22

Expert Comment

by:cookre
Comment Utility
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
 

Author Comment

by:larryle
Comment Utility
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
 

Author Comment

by:larryle
Comment Utility
In what part of my codes above, do I need to plug in your codes?  Please advise.

URGENT Please

Larry
0
 
LVL 22

Expert Comment

by:cookre
Comment Utility
I'd put it in the Leave event for the text box.
0
 

Author Comment

by:larryle
Comment Utility
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
 
LVL 22

Expert Comment

by:cookre
Comment Utility
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
 

Author Comment

by:larryle
Comment Utility
cookre:

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

Larry
0
 

Author Comment

by:larryle
Comment Utility

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
 
LVL 22

Expert Comment

by:cookre
Comment Utility
How'd it go?
0
 

Author Comment

by:larryle
Comment Utility
It did not go better since last night where you left off.  
0
 
LVL 22

Expert Comment

by:cookre
Comment Utility
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
 

Author Comment

by:larryle
Comment Utility
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
 
LVL 22

Accepted Solution

by:
cookre earned 500 total points
Comment Utility
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
 

Author Comment

by:larryle
Comment Utility
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
 
LVL 22

Expert Comment

by:cookre
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:larryle
Comment Utility


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
 
LVL 22

Expert Comment

by:cookre
Comment Utility
Look back at the most recent code I posted.
When does it display "Yummie"?
0
 

Author Comment

by:larryle
Comment Utility

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
 
LVL 22

Expert Comment

by:cookre
Comment Utility
If it doesn't give you "Yummie", something was typed in wrong.  Post your code and let's see.
0
 

Author Comment

by:larryle
Comment Utility

#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
 
LVL 22

Expert Comment

by:cookre
Comment Utility
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
 

Author Comment

by:larryle
Comment Utility
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
 

Author Comment

by:larryle
Comment Utility
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
 
LVL 22

Expert Comment

by:cookre
Comment Utility
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
 

Expert Comment

by:khubpk
Comment Utility
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
 

Author Comment

by:larryle
Comment Utility

Great.  Thanks and I really appreciate it.

Larry
0
 

Expert Comment

by:Snapa02
Comment Utility
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
 
LVL 2

Expert Comment

by:jtbalogh
Comment Utility
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
 

Expert Comment

by:Snapa02
Comment Utility
Kind of harsh- but thanks anyway.  I am new here- and did not know.  Noted.

Snapa
0
 
LVL 2

Expert Comment

by:jtbalogh
Comment Utility
I could have rephrased it nicer. Sorry.
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

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
A short article about problems I had with the new location API and permissions in Marshmallow
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

744 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

11 Experts available now in Live!

Get 1:1 Help Now