numbers only textbox

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
 
K

Ken Tucker [MVP]

Hi,

Regular expression to validate positive numbers.

Dim rNumber As New
System.Text.RegularExpressions.Regex("(^\d*\.?\d*[1-9]+\d*$)|(^[1-9]+\d*\.\d*$)")
Dim strValid() As String = {"$123.44", "1234.22", "232..232",
"2324.332", "&H244"}

For x As Integer = 0 To strValid.GetUpperBound(0)
Trace.WriteLine(String.Format("{0} matches {1}",
strValid(x), _
rNumber.IsMatch(strValid(x))))
Next

Ken
-------------------
 
G

Guest

OR

Private Sub NumbValidation_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtBox1.KeyPress, txtBox2.KeyPress
If (Not Char.IsDigit(e.KeyChar) And Not Char.IsControl(e.KeyChar)) Or Char.IsPunctuation(e.KeyChar) Then
If e.KeyChar = Char.Parse("-") Or e.KeyChar = Char.Parse(".") Then
e.Handled = False
Else
e.Handled = True
End If
End If
End Sub
 
G

Greg Burns

Yeah, but I don't want to suppress keypresses. I want to display an error
after they type invalid data. (I can't stand textboxes that do that, but
that's just me).

Greg

BrianDH said:
OR

Private Sub NumbValidation_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles txtBox1.KeyPress,
txtBox2.KeyPress
If (Not Char.IsDigit(e.KeyChar) And Not Char.IsControl(e.KeyChar)) Or
Char.IsPunctuation(e.KeyChar) Then
 
G

Greg Burns

Doesn't work with 1,000. But I can live with that! In fact, this is
probably best.

Thanks Ken.


Ken Tucker said:
Hi,

Regular expression to validate positive numbers.

Dim rNumber As New
System.Text.RegularExpressions.Regex("(^\d*\.?\d*[1-9]+\d*$)|(^[1-9]+\d*\.\d
*$)")
Dim strValid() As String = {"$123.44", "1234.22", "232..232",
"2324.332", "&H244"}

For x As Integer = 0 To strValid.GetUpperBound(0)
Trace.WriteLine(String.Format("{0} matches {1}",
strValid(x), _
rNumber.IsMatch(strValid(x))))
Next

Ken
-------------------

Greg Burns said:
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
 
H

Herfried K. Wagner [MVP]

* "Greg Burns said:
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. :)

Have a look at 'Double.TryParse'.
 
A

Amaryllis

Hi Greg,

Just an idea, but have you tried just searching the string to see if
certain special characters have been entered? Built-in functions
like IndexOf and inStr are good for that. I'm not sure what the
purpose of your code is, so those functions might not be efficient,
but again, just food for thought.

Amaryllis
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top