Only allowing 2 decimal places in a text box
'Ello,
I was wondering is there any fairly easy way to restrict the numbers a user inputs into a text box so there can be no more than two decimal places? I am sure there is away of doing it, but I am just looking for a fairly easy way, if there isn't I can get around it easily, but I just wanted to know if I could only allow them to enter in two decimal places into a text box.
Re: Only allowing 2 decimal places in a text box
can't you use a mask to set the field format?
Re: Only allowing 2 decimal places in a text box
You can use Regex:
Forces two decimal places, everything left of the decimal is optional digits.
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim TwoDec As New System.Text.RegularExpressions.Regex("^(\d)*\.\d{2}$")
If TwoDec.IsMatch(TextBox1.Text) Then
MessageBox.Show("Yay!")
Else
MessageBox.Show("Nuh huh!")
End If
End Sub
Or if you want it so they just can't have more then two decimals, use this: as the RegEx:
Dim TwoDec As New System.Text.RegularExpressions.Regex("^(\d)*(\.\d{2})?$")
Re: Only allowing 2 decimal places in a text box
Why theres a control already set to do this, the NumericUpDown control, just set the number of decimal places to 2, and it only allows numbers to be entered in the box... no letters... :)
Re: Only allowing 2 decimal places in a text box
With NumericUpDown, you would still have to validate. (As far as I know) it will still alow you to put your own numeric values in it with more than 2 decimals (even if you specify only two, that only effects the incrementing).
Re: Only allowing 2 decimal places in a text box
VB Code:
Private Sub txtValue_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtValue.KeyPress
If txtvalue.Text.Length > 0 Then
Dim ary As Array = txtValue.Text.Split(".")
If ary.Length > 1 Then
If CType(ary(1), String).Length > 1 Then
e.Handled = True
End If
End If
End If
End Sub
Re: Only allowing 2 decimal places in a text box
If you want to prevent invalid input in the first place then you could use something like this:
VB Code:
Private Sub myTextBox_KeyPress(...) Handles myTextBox.KeyPress
If myTextBox.Text.IndexOf("."c) <> -1 AndAlso _
myTextBox.Text.Length - myTextBox.Text.IndexOf("."c) AndAlso _
myTextBox.SelectionStart > myTextBox.Text.IndexOf("."c) AndAlso _
myTextBox.SelectionLength = 0
'There are already two decimal places and the user is trying to add another so reject the character.
e.Handled = True
Beep()
End If
End Sub
This code checks whether there is a decimal point, that there are two decimal places already and the the user is inserting text and not replacing. If all this is true then the new character is rejected. This would be in addition to your usual numerical validation. It is better to prevent invalid input in the first place than allow it and then try to fix it later.
Re: Only allowing 2 decimal places in a text box
That's pretty much what my code was doing. Heh, oh well, just coding preference.
Re: Only allowing 2 decimal places in a text box
Quote:
Originally Posted by MetalKid
That's pretty much what my code was doing. Heh, oh well, just coding preference.
You hadn't posted your code when I started typing mine. :) Note though that your code doesn't care where the cursor is. Once you have two digits after the decimal point your code won't let you add another digit anywhere. What if you want to add another digit before the decimal point? Also, what if you want to select several characters and replace them? Your code does not account for any of that. This is one of those situations where you need to spend a bit of time thinking about not only the most usual case but also the less usual cases. If there is ANY possibility of a set of circumstances occurring then you should account for them, and those cases I've mentioned wouldn't necessarily be all that unusual.
1 Attachment(s)
Re: Only allowing 2 decimal places in a text box
I use a simple control made by myself, it seems to work, but I can't ensure that, because it was not deeply tested. Using it, you can specify 2,3 or 4 decimal digits and which kind of separator you need (Italian or American) so you can obtain: 123.456,58 (Italian), or 123,456.58 (American). It has a limit at 999,999,999.9999. Comments are in italian. Code is written in VB2005 but it comes from 2003 with few changes. You can use part of it, or totally put it in your own control as I did. It's not a very optimized code, probably it's possible do a clearer job. It's something like 'quick and dirty'! ;)