Results 1 to 4 of 4

Thread: Ignoring 1s and 0s in a TextBox

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2012
    Posts
    3

    Ignoring 1s and 0s in a TextBox

    I'm new to Visual Basic express 2010 and I'm trying to figure out a tiny code to get my TextBox to ignore 1s and 0s and clear the TextBox.
    This is what I have currently :

    Code:
    If (txtPhoneNumber.Text) = ("1") Or ("0") Then
                        txtPhoneNumber.Text = ""
                        MessageBox.Show("Phone Numbers Must Not Contain 1s or 0s!", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    End If
    The current code doesn't work at all so I'm looking for something that will.

    Thank you in advance for looking!
    Last edited by OmeshPH; Aug 3rd, 2012 at 10:50 PM.

  2. #2
    New Member
    Join Date
    Aug 2012
    Posts
    12

    Re: Ignoring 1s and 0s in a TextBox

    Try this code:

    Code:
    if txtPhoneNumber.Text.Contains("0") OR txtPhoneNumber.Text.Contains("1") Then
                        txtPhoneNumber.Text = ""
                        MessageBox.Show("Phone Numbers Must Not Contain 1s or 0s!", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    End If
    The reason your code isn't working is because you are looking at the whole string. So if you were to type in just 1 and 0 into the box your code would work but if you did a bunch of numbers and then a 1 or 0 it would not work. That is because the random numbers plus the 1 or 0 does not equal just 1 or 0
    Last edited by waddle1463; Aug 3rd, 2012 at 11:36 PM.

  3. #3
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Ignoring 1s and 0s in a TextBox

    Your current code is only checking if the text in the box contains only a 1 or a 0 so it would fail if there were 2 or more digits no matter where or how many 1s and/or 0s were there.

    You could use the .IndexOf() to check if a string contains an instance of a character or string but I am not sure why you would want to filter out 1s and 0s from phone numbers as both are valid. For example my phone number has 2 0s in it. It is even possible that someones number is nothing but 1s and 0s and very likely that any given phone number will contain at least one 0 or 1 so you would be making it impossible to enter millions of valid phone numbers.

  4. #4

    Thread Starter
    New Member
    Join Date
    Aug 2012
    Posts
    3

    Re: Ignoring 1s and 0s in a TextBox

    I'm making a Telephone Word Generator for a homework project and since 1 and 0 has no Letters I don't need them inputted.

    Also gonna try your code waddle and let you know if it works

    Thank You waddle it worked exactly as I was intending it to work !
    Last edited by OmeshPH; Aug 3rd, 2012 at 11:43 PM. Reason: confirming code works

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