Visual Studio Development Bookmark and Share   
 index > Visual Basic Express Edition > checking if a textbox contains numbers
 

checking if a textbox contains numbers

i would like to know how to check if a textbox contains numbers i tried the following

If IsNumeric(CategoryTextBox.Text) = True Then

MessageBox.Show("Please delete the digit", "Name warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

End If

but it only works ifthere is no lettersin the textbox for example "11212" it doesnt work if i have something like this "1adasds" or "asd6asdd" so please help

dom96  Monday, June 16, 2008 5:04 PM

Dim chars() As Char = CategoryTextBox.Text

For Each c As Char In chars

If IsNumeric(c) Then

MessageBox.Show("Please delete the digit", "Name warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

Exit For

End If

Next

bdbodger  Monday, June 16, 2008 5:09 PM

Dim chars() As Char = CategoryTextBox.Text

For Each c As Char In chars

If IsNumeric(c) Then

MessageBox.Show("Please delete the digit", "Name warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

Exit For

End If

Next

bdbodger  Monday, June 16, 2008 5:09 PM

You could do something like this:

Code Snippet

Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
RemoveDigits()
End Sub


Private Sub RemoveDigits()
Dim temp As String = String.Empty
For i As Integer = 0 To textBox1.Text.Length - 1
If Not IsNumeric(textBox1.Text.Substring(i, 1)) Then
temp += textBox1.Text.Substring(i, 1)
End If
Next
textBox1.Text = temp
End Sub

JohnGrove  Monday, June 16, 2008 5:27 PM

thanks bdbodger that works great

dom96  Monday, June 16, 2008 5:31 PM

As you can see, there are many solutions to this question. Just in case you wanted another one:

Code Snippet

Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

If Not Char.IsControl(e.KeyChar) And Not Char.IsDigit(e.KeyChar) Then e.Handled = True

End Sub

This prevents anything that isn't a digit or a control character (such as Tab, Backspace, etc.) from being entered in TextBox1. Stuff still might be pasted in there, though. If you're worried about that, test the whole string for Not(IsNumeric(CType(sender, Textbox).Text)) instead.

Squire James  Monday, June 16, 2008 6:08 PM

IsNumeric is filled with bugs. Regular expressions would be the best choice:

Code Snippet

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

If System.Text.RegularExpressions.Regex.IsMatch(TextBox1.Text, "\d+") Then

MessageBox.Show("Please delete the digit", "Name warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

End If

End Sub

Code Only  Monday, June 16, 2008 7:11 PM

No Adam, IsNumeric is not full of bugs.

ReneeC  Monday, June 16, 2008 7:24 PM

Man, this guy has major psychological disorders. He truly is a stalker...

Forum members beware.....Adam is back

JohnGrove  Monday, June 16, 2008 7:29 PM

Do NOT use IsNumeric to test for presence of all digits. Very few people realize that IsNumeric has multiple features, including the ability to test for hexadecimal values. For example, if the text box includes: &H43

it will be True for IsNumeric. It will also return True for a type Double value, so if you need to test for an integer number, it won't alwayswork.

Better to test each character one by one using the Char.IsDigit method, as pointed out by one of the posters above. You could also use theChar.IsLetter method to solve the problem.

For the opposite problem (which I am mentioning here as general information), you could use the TryParse method to test for an integer, which must include all digits (but can also include a leading minus symbol, which you will have to test for):

Dim num As Integer

If Integer.TryParse(TextBox1.Text, num) Then

MessageBox.Show("OK")

EndIf

Solitaire  Monday, June 16, 2008 8:31 PM

My own version of IsNumeric didn't find that to be true:

public static bool IsNumeric(object Expression)

{

bool isNum;

double retNum;

isNum = Double.TryParse(Convert.ToString(Expression), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum);

return isNum;

}

JohnGrove  Monday, June 16, 2008 8:41 PM

Code Snippet

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

TextBox1.Text = "&H43"

If IsNumeric(TextBox1.Text) Then MessageBox.Show("Text in box is numeric")

End Sub

Solitaire  Monday, June 16, 2008 10:37 PM

Solitaire,

The user is not checking the textbox's whole text as a string, but each character. And using my above method would still remove just the digits:

Private Sub RemoveDigits()
Dim temp As String = String.Empty
For i As Integer = 0 To textBox1.Text.Length - 1
If Not IsNumeric(textBox1.Text.Substring(i, 1)) Then
temp += textBox1.Text.Substring(i, 1)
End If
Next
textBox1.Text = temp
End Sub

JohnGrove  Monday, June 16, 2008 10:40 PM

I know that. I merely included it as general information. I did say that Char.IsDigit or Char.IsLetter should be used to check each character.

Solitaire  Tuesday, June 17, 2008 3:22 AM
Thanks Sol,
Usefull info

regards
rajeev
rajeev_learning_vb2005  Tuesday, June 17, 2008 3:50 AM

Yes it is good info, as I did not know it regarded hexadecimals as values. But I am not a VB developer, i am a C# developer, so I had to create my own IsNumeric..

Thanks Solitaire

JohnGrove  Tuesday, June 17, 2008 4:14 AM

You can use google to search for other answers

Custom Search

More Threads

• Reeorder variables by value
• Error 1 'DataSource' is not a member of 'SpreadsheetGear.Windows.Forms.WorkbookView'.
• Addition of all For-Next Loop variables
• How can I create a SIMPLE Report in VB Express?
• Saving data to a data base in VB2008 on 64b Vista
• Textbox Validation
• Help with streamReader and Combo Box
• Linking To A Web Page From Button
• How to ping a ip every minute and show results in a form.
• How to change disabled combo and datepicker background?