How to check the data type of user input?VB.Net
-
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...
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#
-
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#
-
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...
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#
-
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...
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. -
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.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... -
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...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.