Results 1 to 10 of 10

Thread: SubString/Number Validation

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2004
    Posts
    19

    SubString/Number Validation

    I am VERY new to VB.Net programming and am having a problem with an Overload exception due to the way I built my strings/substrings. Here's the first part of my code causing the problem:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    PhoneNum = TextBox1.Text
    Num1 = PhoneNum.Substring(0, 3)
    Num2 = PhoneNum.Substring(4, 3)
    Num3 = PhoneNum.Substring(8)
    If Not IsNumeric(Num1) Then
    MessageBox.Show( _
    "The phone number you entered " & TextBox1.Text & " is not valid." _
    & " ERROR: It must conatin only numbers.", "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error, _
    MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly)
    ElseIf Not IsNumeric(Num2) Then
    MessageBox.Show( _
    "The phone number you entered " & TextBox1.Text & " is not valid." _
    & " ERROR: It must conatin only numbers.", "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error, _
    MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly)
    ElseIf Not IsNumeric(Num3) Then.....

    If I do not enter enough characters to satisfy the substrings, I get the Overlaod exception. PLEASE ADVISE and Thanks in advace.

    Christos

  2. #2
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Homework or workplace project?

    Answer depends on this, because you may get an F if you use RegEx's

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Apr 2004
    Posts
    19
    This is homework and and I'm supposed to be using SubString and Length methods as well as IndexOf to filter the "-". I think I got most of the coding fixed by replacing the .Substring with .IndexOf for the Num1, 2 & 3.

  4. #4
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    Dear Christos, before of any other thing I want to advise you that is possible I have misunderstood the problem. It should not be the first time that my english fail!

    Anyway, if you need that the text of your textbox were composed by a minimum number of characters, because, if it is not so, at least Num3 will result empty, and then surely not numeric, why do you not test the entire number at the beginning before to assign the substring?

    To filter "-", you can:

    Dim BigNum as string=textbox1.text
    BigNum.Replace("-","")

    Obviously you can repeat for others characters usable in place of "-", if any.

    But you can also use your approach, dividing the text in 3 substring, but only after you have tested if textBox1.text.lenght is sufficient

    And then, you could simplify your code with a group test like this:

    If (IsNumeric(Num1) = False Or IsNumeric(num2) = False Or IsNumeric(Num3) = False) Then

    MessageBox.Show( _
    "The phone number you entered " & TextBox1.Text & " is not valid." _
    & " ERROR: It must conatin only numbers.", "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error, _
    MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly)

    End If



    Or I'm missing something?

    Good job
    Live long and prosper (Mr. Spock)

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Apr 2004
    Posts
    19
    Alextyx,

    I'm not sure what you mean about testing the textbox1.text.length.

    I've attached a text file of my code.

    Christos
    Attached Files Attached Files

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Apr 2004
    Posts
    19
    I think I got it!!!

    Thank You all for your assistance.

    Christo

  7. #7
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    Happy to see that perhaps tour problem are solved, anyway, before to realize that, I built a version of your program that seems to work. At now the job is already done and so I send it to you

    Private Sub Prova()

    Dim PhoneNum As String = TextBox1.Text

    'Before, I need to be sure to have the minimum lenght required, or an error will occur when I will try to extract a substring from nothing!
    If PhoneNum.Length < 12 Then
    MsgBox("The phone number you entered " & TextBox1.Text & " is not valid." & " ERROR: It must contain 12 characters.", MsgBoxStyle.OKOnly)
    Exit Sub
    End If

    'Assume that Textbox1.text is: 039-574-730128 (is my office number, don't dial it! :-)) )

    Dim Num1 As String = PhoneNum.Substring(0, 3) 'Num1="039"
    Dim Num2 As String = PhoneNum.Substring(4, 3) 'Num2="574"
    Dim Num3 As String = PhoneNum.Substring(8) 'Num3="730128"

    If (IsNumeric(Num1) = False Or IsNumeric(Num2) = False Or IsNumeric(Num3) = False) Then
    MsgBox("The phone number you entered " & TextBox1.Text & " is not valid." & " ERROR: It must contain only numbers.", MsgBoxStyle.OKOnly, "Error !")
    Exit Sub

    'I prefer to use "Phonenum" in place of "TextBox1.Text", but it works also with "Textbox1.text"
    ElseIf PhoneNum.Chars(3) <> "-" Then
    MsgBox("The phone number you entered " & TextBox1.Text & " is not valid." & " ERROR: You must enter a dash after the area code.", MsgBoxStyle.OKOnly, "Error !")
    Exit Sub

    ElseIf PhoneNum.Chars(7) <> "-" Then
    MsgBox("The phone number you entered " & TextBox1.Text & " is not valid." & " ERROR: You must enter a dash after the area code.", MsgBoxStyle.OKOnly, "Error !")
    Exit Sub

    Else
    MsgBox("The phone number you entered " & TextBox1.Text & " is a valid phone number.", MsgBoxStyle.OKOnly, "Phone number validation message")
    End If

    TextBox1.Focus()
    TextBox1.Text = ""

    End Sub

    Good job
    Live long and prosper (Mr. Spock)

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Apr 2004
    Posts
    19
    My last question on this...
    Is there a way I can filter the dashes form the last MessageBox using IndexOf ?

    Christo

  9. #9
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    Uhmmm, I'm not sure to understand your request.
    In order to filter dashes, remain valid my first post: you can use the replace method!

    PhoneNum.Replace("-","")

    It replaces the UNICODE character on the left of the comma, with the one on the right of the comma, everytime it occurs in the string.

    You can use index of, followed by other instructions, but I don't see the reason of this choice!

    If I have not answered properly (because I did not understand what you were asking to me), try to explain your problem, once more.
    Live long and prosper (Mr. Spock)

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Apr 2004
    Posts
    19
    I'm not exactly sure what I'm supposed to do either.

    I certainly appreciate all your help.

    I'll be back next week seeking help on arrays.

    Christo

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width