Algar Posted July 31, 2003 Posted July 31, 2003 (edited) 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: 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 ? Edited July 31, 2003 by Algar Quote
Algar Posted July 31, 2003 Author Posted July 31, 2003 Ok so I forgot about IsNumeric.... my bad :) Quote
Leaders dynamic_sysop Posted July 31, 2003 Leaders Posted July 31, 2003 to check if it's all numbers or not without getting the individual chars : 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 : 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 :) Quote
mukesh Posted August 26, 2006 Posted August 26, 2006 Thanks Ok so I forgot about IsNumeric.... my bad :) Thanks, Realy I forgot about IsNumeric function. I am New with .Net. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.