Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. How to check the data type of user input?VB.Net

How to check the data type of user input?VB.Net

Scheduled Pinned Locked Moved Visual Basic
helpquestioncsharptutorial
7 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MJay
    wrote on last edited by
    #1

    as topic, how do i check the data type (integer and string) of the user input in a textbox?? if i wan the user to input number 123, but he/she entered 123A, how i going to detect there is an error of input?? any help is greatly appreciate...

    C C 2 Replies Last reply
    0
    • M MJay

      as topic, how do i check the data type (integer and string) of the user input in a textbox?? if i wan the user to input number 123, but he/she entered 123A, how i going to detect there is an error of input?? any help is greatly appreciate...

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      Try something like:

      Dim MyInteger As Int32

      Try
      MyInteger = Convert.ToInt32(Me.TextBox1.Text)
      Catch fex As FormatException
      ' Do something here to handle the input being wrong
      ' like "123A"
      Catch oex As OverflowException
      ' Do something here to handle the input being out of
      ' range (i.e. It is a number but is it too big for
      ' an 32 bit integer.
      End Try

      --Colin Mackay--

      "In the confrontation between the stream and the rock, the stream always wins - not through strength but perseverance." (H. Jackson Brown) Enumerators in .NET: See how to customise foreach loops with C#

      M 1 Reply Last reply
      0
      • C Colin Angus Mackay

        Try something like:

        Dim MyInteger As Int32

        Try
        MyInteger = Convert.ToInt32(Me.TextBox1.Text)
        Catch fex As FormatException
        ' Do something here to handle the input being wrong
        ' like "123A"
        Catch oex As OverflowException
        ' Do something here to handle the input being out of
        ' range (i.e. It is a number but is it too big for
        ' an 32 bit integer.
        End Try

        --Colin Mackay--

        "In the confrontation between the stream and the rock, the stream always wins - not through strength but perseverance." (H. Jackson Brown) Enumerators in .NET: See how to customise foreach loops with C#

        M Offline
        M Offline
        MJay
        wrote on last edited by
        #3

        thanks again for help this solution is works for checking integer, but seems like doest work on character? i need to check a user input whether it is charecter type... ur help is greatly appreciated...

        C 1 Reply Last reply
        0
        • M MJay

          thanks again for help this solution is works for checking integer, but seems like doest work on character? i need to check a user input whether it is charecter type... ur help is greatly appreciated...

          C Offline
          C Offline
          Colin Angus Mackay
          wrote on last edited by
          #4

          Sorry, I'm don't quite understand. I thought you wanted to check that the input was an integer. Is this another control you want to check? Can you be more specific about what you need it to do. Thanks. --Colin Mackay--

          "In the confrontation between the stream and the rock, the stream always wins - not through strength but perseverance." (H. Jackson Brown) Enumerators in .NET: See how to customise foreach loops with C#

          1 Reply Last reply
          0
          • M MJay

            as topic, how do i check the data type (integer and string) of the user input in a textbox?? if i wan the user to input number 123, but he/she entered 123A, how i going to detect there is an error of input?? any help is greatly appreciate...

            C Offline
            C Offline
            Charlie Williams
            wrote on last edited by
            #5

            One way to validate user input is using regular expressions. Regexes can get pretty complicated if you need them to do more advanced validation, but simple things like making sure only numbers are input are simple enough. This test looks for the beginning of a line, followed by one or more numbers, followed by the end of the line: Imports System.Text.RegularExpressions Dim re as new Regex("^\d+$") If re.IsMatch(myTextBox.Text) Then 'Input passed the test Else 'Failed End If To accept only non-numbers: "^\D+$" To accept only strings in the format 123A: "^[\d]{3}[A-Z]$" There are more variations than you can shake a stick at. You could, for example, make sure input is in the format of a US social security number or a phone number or a URL or whatever else you can think of. Charlie Here I am. Love me.

            M 1 Reply Last reply
            0
            • C Charlie Williams

              One way to validate user input is using regular expressions. Regexes can get pretty complicated if you need them to do more advanced validation, but simple things like making sure only numbers are input are simple enough. This test looks for the beginning of a line, followed by one or more numbers, followed by the end of the line: Imports System.Text.RegularExpressions Dim re as new Regex("^\d+$") If re.IsMatch(myTextBox.Text) Then 'Input passed the test Else 'Failed End If To accept only non-numbers: "^\D+$" To accept only strings in the format 123A: "^[\d]{3}[A-Z]$" There are more variations than you can shake a stick at. You could, for example, make sure input is in the format of a US social security number or a phone number or a URL or whatever else you can think of. Charlie Here I am. Love me.

              M Offline
              M Offline
              MJay
              wrote on last edited by
              #6

              thanks for help what if i wan to accept only character A-Z and a-z ?(no included symbols such as @#*%_) wat is the regularexpression format? To accept only non-numbers: "^\D+$" this format accept only character but it also accept symbols which i dun wan be included... ur help is greatly appreciated...

              C 1 Reply Last reply
              0
              • M MJay

                thanks for help what if i wan to accept only character A-Z and a-z ?(no included symbols such as @#*%_) wat is the regularexpression format? To accept only non-numbers: "^\D+$" this format accept only character but it also accept symbols which i dun wan be included... ur help is greatly appreciated...

                C Offline
                C Offline
                Charlie Williams
                wrote on last edited by
                #7

                Anything in square brackets will match one character. The hyphen specifies a range. For example, [a-m] will match 'a' or 'g', etc., but not 'n' or 'A' (It's case sensitive, although there's an \i switch to compare in a case insensitive manner.) So [a-zA-Z] is what you're looking for. \w is similar, but also matches _ and 0-9 Mastering Regular Expressions[^] by Jeffrey E. F. Friedl is a great book about regexes. The author uses perl for the bulk of the book, but there is a section specific to .net and .net uses mostly perl flavored regexes, anyway. (Though in an OO way). If you don't use them enough to justify purchasing a book, VS.Net's online help has quite a bit of info on them. Charlie Here I am. Love me.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups