G
Greg Burns
I have a textbox that I only want to allow "numbers".
1,000
1000
1000.0512
I don't want to prevent them from typing anything else, because I want to
check their input in the Validating event and will display an error with an
ErrorProvider.
First attempt (from my TextBox1 Validating event):
If TextBox1.Text = String.Empty Then
ErrorProvider1.SetError(TextBox1, "Required")
ElseIf Not IsNumeric(TextBox1.Text) Then
ErrorProvider1.SetError(TextBox1, "Not valid")
Else
ErrorProvider1.SetError(TextBox1, "")
End If
The problem with this is that "$1.00" passes the IsNumeric test. IsNumeric
considers currency a number, while I don't.
Second attempt:
If TextBox1.Text = String.Empty Then
ErrorProvider1.SetError(TextBox1, "Required")
ElseIf Not IsNumeric(TextBox1.Text) Then
ErrorProvider1.SetError(TextBox1, "Not valid")
Else
Try
Decimal.Parse(txtVacRemaining.Text)
ErrorProvider1.SetError(TextBox1, "")
Catch
ErrorProvider1.SetError(TextBox1, "Not valid")
End Try
End If
This works, but feels like a hack. Anybody have a better solution?
TIA,
Greg
1,000
1000
1000.0512
I don't want to prevent them from typing anything else, because I want to
check their input in the Validating event and will display an error with an
ErrorProvider.
First attempt (from my TextBox1 Validating event):
If TextBox1.Text = String.Empty Then
ErrorProvider1.SetError(TextBox1, "Required")
ElseIf Not IsNumeric(TextBox1.Text) Then
ErrorProvider1.SetError(TextBox1, "Not valid")
Else
ErrorProvider1.SetError(TextBox1, "")
End If
The problem with this is that "$1.00" passes the IsNumeric test. IsNumeric
considers currency a number, while I don't.

Second attempt:
If TextBox1.Text = String.Empty Then
ErrorProvider1.SetError(TextBox1, "Required")
ElseIf Not IsNumeric(TextBox1.Text) Then
ErrorProvider1.SetError(TextBox1, "Not valid")
Else
Try
Decimal.Parse(txtVacRemaining.Text)
ErrorProvider1.SetError(TextBox1, "")
Catch
ErrorProvider1.SetError(TextBox1, "Not valid")
End Try
End If
This works, but feels like a hack. Anybody have a better solution?
TIA,
Greg