Page 1 of 1

if textbox have numbers then ...

Posted: Tue Jan 14, 2014 3:09 am
by troop
hi im making a program now im stuck
i wanna make a simple code to check if
textbox1.text has numbers and have
no letters then it will msgbox else it will
do nothing

i will make a fake code to make it easier

if textbox1.text have numbers and dont have letters or characters then
msgbox("yolo")

thnx :?

Re: if textbox have numbers then ...

Posted: Tue Jan 14, 2014 3:18 am
by noypikami
try this code:::
Code: Select all
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim value As Integer

        Dim text As String = TextBox1.Text
        If Integer.TryParse(text, value) Then
            MsgBox(value.ToString)
        Else
            MsgBox("the value is not an integer")
        End If
    End Sub

Re: if textbox have numbers then ...

Posted: Tue Jan 14, 2014 3:21 am
by troop
noypikami wrote:
try this code:::
Code: Select all
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim value As Integer

        Dim text As String = TextBox1.Text
        If Integer.TryParse(text, value) Then
            MsgBox(value.ToString)
        Else
            MsgBox("the value is not an integer")
        End If
    End Sub
thanks lol i didnt expect anyone to answer me this early, im going to school now when i come back i will tell you if it works
loove; loove; cooll;

Re: if textbox have numbers then ...

Posted: Tue Jan 14, 2014 3:21 am
by Shim
Hi,

Try this

Function :
Code: Select all
 Private Function IsInputNumeric(input As String) As Boolean
        If String.IsNullOrWhiteSpace(input) Then Return False
        If IsNumeric(input) Then Return True
        Dim parts() As String = input.Split("/"c)
        If parts.Length <> 2 Then Return False
        Return IsNumeric(parts(0)) AndAlso IsNumeric(parts(1))
    End Function
Usage :
Code: Select all
If IsInputNumeric(TextBox1.Text) Then
            'handle numeric input
        Else
            'handle not a number
        End If
Source : http://stackoverflow.com/a/15423718/2443148

Re: if textbox have numbers then ...

Posted: Tue Jan 14, 2014 11:05 am
by comathi
#Shim, don't forget to mention this code will also allow slashes ("/") ;)