texting strings for non-numbers

Algar

Freshman
Joined
Jun 17, 2003
Messages
28
Location
Toronto
With all these new .NET things im wondering whats the best way to test if a textbox contains any non-numbers.
The best I came up with was:
Visual Basic:
Dim i As Integer = TexBox.Text.Length
Dim ctr As Integer

For ctr = 1 To i
	If Asc(TextBox.Text.Chars(ctr - 1)) > 57 Or Asc(sender.Text.Chars(ctr - 1)) < 48 Then
				'its not a number
	End If
Next
Seems like there must be a way to improve on this ?
 
Last edited:
to check if it's all numbers or not without getting the individual chars :
Visual Basic:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Not IsNumeric(TextBox1.Text) Then
            MessageBox.Show("it contains characters other than numeric!")
        End If
End Sub
or a better way , checking each char :
Visual Basic:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim ch As Char, chResult As Char, x As Integer

        For Each ch In TextBox1.Text
            If Not ch.IsNumber(ch) Then
                x += 1
                chResult += ch & ","
            End If
        Next
        MessageBox.Show("There were: " & x & " None-Numerics in the textbox, they were: " & chResult)
    End Sub
hope it helps :)
 
Back
Top