locked
Verify input data type RRS feed

  • Question

  • Hi everyone, this is my first time messing with visual basic. I'm just learning how to do different things and right now I'm building a simple program to where the user inputs thier First and last name into two different text boxes and when they click on a button it tells them what they have entered.

    I'm trying to make sure that they have entered letters only in each text box and if they don't I want it to display a message box that says they need to enter letters only. The problem I am having is if they enter a number in both text boxes then that is the only way that it will display the error message box. Here is my code for the data validation for the user input...

     

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles

    Button1.Click

     

    If IsNumeric(TextBox1.Text)

    And

    IsNumeric(TextBox2.Text)

    Then

     

    MessageBox.Show("I'm sorry, the data you entered is not of the correct type. Please enter leters only.", "Wrong Data Type Entered", MessageBoxButtons.OK, MessageBoxIcon

    .Error)

     

    Else

    MsgBox(

    "You have entered " & TextBox1.Text & " "

    & TextBox2.Text)

     

    End

    If

     

    End

    Sub

     

     

    Any help you can give would be much appreciated, thanks! 

    Friday, November 11, 2011 12:45 AM

Answers

  • IsNumeric will only return True if the text can be converted to a numeric value. If the text contains digits but they do not represent a valid number, IsNumeric will return False.

    Use code like this:
            Dim TestText As String = TextBox1.Text & TextBox2.Text
            
    For Each c As Char In TestText
                
    If Char.IsDigit(c) Then
                    MessageBox.Show("I'm sorry, the data you entered is not of the correct type. Please enter leters only.""Wrong Data Type Entered", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    
    Exit Sub
                End If
            Next
            MsgBox("You have entered " & TextBox1.Text & " " & TextBox2.Text)

    Note that your example does not agree with your description.   To be strictly as per your description the test should be:

            If Not Char.IsLetter(c) Then

    • Proposed as answer by Hardz Tarrayo Friday, November 11, 2011 2:44 AM
    • Marked as answer by Mike Feng Monday, November 21, 2011 6:09 PM
    Friday, November 11, 2011 2:08 AM
  • Hi you can condition check if user has entered anything, if nothing or space then show error message otherwise show what they entered. You can use the textbox keypress to allow only what you want. Also there is validating event where you can force to remain in that control if user enter only space or nothing:)

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If String.IsNullOrWhiteSpace(TextBox1.Text) Or String.IsNullOrWhiteSpace(TextBox2.Text) Then
                MessageBox.Show("I'm sorry, the data you entered is not of the correct type. Please enter leters only.", "Wrong Data Type Entered", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Else
    
                MsgBox("You have entered: " & StrConv(TextBox1.Text & ", " & TextBox2.Text, VbStrConv.ProperCase))
    
            End If
    
        End Sub
    

    Just a thought my name can either be Xiong Wei, Jin or Xiongwei, Jin if the middle name is elimined then there are few hundreds whom has the same name, one time Blackwood has pointed out that there are people who has up to 4 or 5.^_^


    Be a good forum member. Make this forum a great place to meet and interact with others around the world.
    • Proposed as answer by Mike Feng Sunday, November 13, 2011 12:11 PM
    • Marked as answer by Mike Feng Monday, November 21, 2011 6:09 PM
    Friday, November 11, 2011 8:23 PM

All replies

  • Use Or in place or And, but what about if user enter space?

     


    Be a good forum member. Make this forum a great place to meet and interact with others around the world.
    Friday, November 11, 2011 1:10 AM
  • Well that's another one of the things that I'm trying to figure out because I just tried entering nothing and it will display the message box without any user input. 

    Is there a good way to code this as to where anything other than alphabetical letters or even nothing entered will display the error message box once the button is clicked?

    Friday, November 11, 2011 2:04 AM
  • IsNumeric will only return True if the text can be converted to a numeric value. If the text contains digits but they do not represent a valid number, IsNumeric will return False.

    Use code like this:
            Dim TestText As String = TextBox1.Text & TextBox2.Text
            
    For Each c As Char In TestText
                
    If Char.IsDigit(c) Then
                    MessageBox.Show("I'm sorry, the data you entered is not of the correct type. Please enter leters only.""Wrong Data Type Entered", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    
    Exit Sub
                End If
            Next
            MsgBox("You have entered " & TextBox1.Text & " " & TextBox2.Text)

    Note that your example does not agree with your description.   To be strictly as per your description the test should be:

            If Not Char.IsLetter(c) Then

    • Proposed as answer by Hardz Tarrayo Friday, November 11, 2011 2:44 AM
    • Marked as answer by Mike Feng Monday, November 21, 2011 6:09 PM
    Friday, November 11, 2011 2:08 AM
  • Hi you can condition check if user has entered anything, if nothing or space then show error message otherwise show what they entered. You can use the textbox keypress to allow only what you want. Also there is validating event where you can force to remain in that control if user enter only space or nothing:)

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If String.IsNullOrWhiteSpace(TextBox1.Text) Or String.IsNullOrWhiteSpace(TextBox2.Text) Then
                MessageBox.Show("I'm sorry, the data you entered is not of the correct type. Please enter leters only.", "Wrong Data Type Entered", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Else
    
                MsgBox("You have entered: " & StrConv(TextBox1.Text & ", " & TextBox2.Text, VbStrConv.ProperCase))
    
            End If
    
        End Sub
    

    Just a thought my name can either be Xiong Wei, Jin or Xiongwei, Jin if the middle name is elimined then there are few hundreds whom has the same name, one time Blackwood has pointed out that there are people who has up to 4 or 5.^_^


    Be a good forum member. Make this forum a great place to meet and interact with others around the world.
    • Proposed as answer by Mike Feng Sunday, November 13, 2011 12:11 PM
    • Marked as answer by Mike Feng Monday, November 21, 2011 6:09 PM
    Friday, November 11, 2011 8:23 PM