Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (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 by Algar
  • Leaders
Posted

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 :)

  • 3 years later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...