Thursday, May 26, 2005

 

TextBox Validating Number of Decimal Places

So one of the requirements that I had was to validate that my TextBox controls in ASP.NET accept only positive numbers with 3 or fewer decimal places. It is easy to use one of the CompareValidator controls to find out if a given entry is an Integer or a Double, as that is well documented. However, neither of these validates for 3 or less decimals; only for 0 or unlimited decimals. So, I just used a RegularExpressionValidator with the following regular expression:
-?((\d*\.\d{0,3})|(\d+))
After that was created, the requirements for the program changed and what was needed was to allow the user to enter in any number of decimal places they wanted, but then the number entered had to be trimmed to have a maximum of only 0 or 3 decimal places, depending on the TextBox. Of course other TextBox controls accept plain text and do not need to be trimmed.

I despise code that uses large Select Case statements or large amounts of If statements, so I wanted a solution that was dynamic. This was the easiest thing that I could think of:
  1. To any TextBox that needs its value trimmed, add DecimalPlaces="3" (or however many you need) in the asp:TextBox tag for any textbox that you need trimmed.
  2. In the code, check for each TextBox if myTextBox.Attributes("DecimalPlaces") <> "".
Now I ran into the problem of automatically finding all of the textboxes so that I can do the trimming. I did this with a search through all the controls on a page or some smaller container on your page. The following recursive code does this:
   Private Sub TrimTextBoxNumbers(ByVal ctrl As Control)
Dim con As Control
Dim txt As TextBox
Dim value As Double
Dim decplaces As Integer
'loop through each child control
For Each con In ctrl.Controls
'check if this control is a textbox
If TypeOf con Is TextBox Then
txt = CType(con, TextBox)
'check if this textbox specifies a number of decimal places
If txt.Attributes("DecimalPlaces") <> "" Then
'try to trim to the given number of decimal places
Try
decplaces = CInt(txt.Attributes("DecimalPlaces"))
Dim reg As Regex = New Regex("-?((\d*\.\d{0," & decplaces & "})|(\d+))")
Dim m As Match = reg.Match(txt.Text)
If m.Success Then
txt.Text = m.Value
End If
Catch 'just abort if there is an error trimming
End Try
End If
End If
TrimTextBoxNumbers(con)
Next
End Sub
I just call it by something resembling this: TrimTextBoxNumbers(Page). Notice that by using a regular expression instead of converting to a decimal, shifting, flooring and shifting, this maintains the zeros at the right to indicate significant digits. That is ".2504" will be changed to ".250" and not ".25" if there are 3 decimal places required.
Comments:
for vb6, this works
[code]' Set the maximum number of digits before the
' decimal point in the MaxWhole constant. Set
' the maximum number of digits after the decimal
' point in the MaxDecimal constant.
Dim LastPosition As Long
Private Sub Text1_Change()
Static LastText As String
Static SecondTime As Boolean
Const MaxDecimal As Integer = 4
Const MaxWhole As Integer = 2
With Text1
If Not SecondTime Then
If .Text Like "*[!0-9.]*" Or _
.Text Like "*.*.*" Or _
.Text Like "*." & String$(1 + MaxDecimal, "#") Or _
.Text Like String$(MaxWhole, "#") & "[!.]" Then
Beep
SecondTime = True
.Text = LastText
.SelStart = LastPosition
Else
LastText = .Text
End If
End If
End With
SecondTime = False
End Sub
Private Sub Text1_MouseDown(Button As Integer, _
Shift As Integer, X As Single, Y As Single)
With Text1
LastPosition = .SelStart
'Place any other MouseDown event code here
End With
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
With Text1
LastPosition = .SelStart
'Place any other KeyPress checking code here
End With
End Sub [/code]

source: http://groups.google.co.in/group/microsoft.public.vb.general.discussion/browse_thread/thread/47124500283f5c0/c972c6099af05537?lnk=st&q=decimal+number+validation+in+Textbox&rnum=2#c972c6099af05537
 
Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?