Results 1 to 10 of 10

Thread: Only allowing 2 decimal places in a text box

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2005
    Posts
    37

    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.

  2. #2
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951

    Re: Only allowing 2 decimal places in a text box

    can't you use a mask to set the field format?

  3. #3
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    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:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim TwoDec As New System.Text.RegularExpressions.Regex("^(\d)*\.\d{2}$")
    3.         If TwoDec.IsMatch(TextBox1.Text) Then
    4.             MessageBox.Show("Yay!")
    5.         Else
    6.             MessageBox.Show("Nuh huh!")
    7.         End If
    8.     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})?$")

  4. #4
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    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...

  5. #5
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

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

  6. #6
    Fanatic Member MetalKid's Avatar
    Join Date
    Aug 2005
    Location
    Green Bay, Wisconsin
    Posts
    534

    Re: Only allowing 2 decimal places in a text box

    VB Code:
    1. Private Sub txtValue_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtValue.KeyPress
    2.         If txtvalue.Text.Length > 0 Then
    3.             Dim ary As Array = txtValue.Text.Split(".")
    4.             If ary.Length > 1 Then
    5.                 If CType(ary(1), String).Length > 1 Then
    6.                     e.Handled = True
    7.                 End If
    8.             End If
    9.         End If
    10.     End Sub

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Private Sub myTextBox_KeyPress(...) Handles myTextBox.KeyPress
    2.     If myTextBox.Text.IndexOf("."c) <> -1 AndAlso _
    3.        myTextBox.Text.Length - myTextBox.Text.IndexOf("."c) AndAlso _
    4.        myTextBox.SelectionStart > myTextBox.Text.IndexOf("."c) AndAlso _
    5.        myTextBox.SelectionLength = 0
    6.         'There are already two decimal places and the user is trying to add another so reject the character.
    7.         e.Handled = True
    8.         Beep()
    9.     End If
    10. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8
    Fanatic Member MetalKid's Avatar
    Join Date
    Aug 2005
    Location
    Green Bay, Wisconsin
    Posts
    534

    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.

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461

    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'!
    Attached Files Attached Files
    Live long and prosper (Mr. Spock)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width