hello, i want to make a function which checks if a value is a number or not....
i've got this;
But when I try it, and insert a number in the textbox it shows the message box but it adds the value to a list too I don't want it to the listCode:Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click For i As Integer = 0 To Integer.MaxValue If TextBox2.Text = CType(i, String) Then GoTo er Exit Sub Else i = 1 TextBox3.Text = TextBox3.Text.Replace("//Created by Table Viewer v1.0 Beta", " " & ComboBox1.Text & " " & TextBox2.Text & ";" & vbNewLine & "//Created by Table Viewer v1.0 Beta") Exit For End If Next er: MsgBox("The value name can't be an integer!", MsgBoxStyle.Exclamation, "Error") End Sub
[highlight=vbnet]Dim i as string = "329495498"
If Microsoft.VisualBasic.IsNumeric(cint(i)) Then
TextBox3.Text = TextBox3.Text.Replace("//Created by Table Viewer v1.0 Beta", " " & ComboBox1.Text & " " & TextBox2.Text & ";" & vbNewLine & "//Created by Table Viewer v1.0 Beta")
Else
MsgBox("The value name can't be an integer!", MsgBoxStyle.Exclamation, "Error")
End If [/highlight]
Should be like that. If you want to check if the string is alphanumeric (contains alphabets and numbers) then use the following function:
First add this:
[highlight=vbnet]Public Function IsNumeric(ByVal [String] As String) As Boolean
For i As Integer = 0 To [String].Length - 1
If Not (AscW([String].Substring(i, 1)) >= 48 AndAlso AscW([String].Substring(i, 1)) <= 57) Then 'asc value range of numbers
Return False
End If
Next
Return True
End Function[/highlight]
Then this:
[highlight=vbnet]Public Function IsAlphaNumberic(ByVal [String] As String) As Boolean
For i As Integer = 0 To [String].Length - 1
If Not (IsNumeric([String].Substring(i, 1)) OrElse _
(AscW([String].Substring(i, 1)) >= 65 AndAlso AscW([String].Substring(i, 1)) <= 90) OrElse _
(AscW([String].Substring(i, 1)) >= 97 AndAlso AscW([String].Substring(i, 1)) <= 122)) Then
Return False
End If
Next
Return True
End Function
[/highlight]
Then call the IsAlphaNumeric function to check:
[highlight=vbnet]If IsAlphaNumeric("ABC123") Then
Msgbox ("")
Else
Msgbox ("/shofo")
End If [/highlight]
Taken from my post in snippets vault.