|
-
May 13th, 2002, 05:30 AM
#1
Thread Starter
Hyperactive Member
Validating Numerics
using the keypress event I can validate a text box for isdigit (0-9) isletter (A-Z)
What I would really like is to validate for a decimal value, ie
0, 4, 66.12 etc.
is there a simple way of doing this in .net?
(There is an isNumeric but that doesnt allow decimal point and does allow Hex)
Thanks for any help
-
May 13th, 2002, 07:15 AM
#2
Frenzied Member
Maybe you can use the substring or indexof methods to find the period. I dont think there is an easier way, but I may be wrong.
-
May 13th, 2002, 07:20 AM
#3
Thread Starter
Hyperactive Member
thanks for thre reply
I have written a little routine to do it - it just occured to me that ther may have been an easy way. (and being lazy - I thought Id ask here).
-
May 13th, 2002, 07:55 AM
#4
Member
Another way is to create a string of allowed characters and then see if the character that corresponds to the pressed key is in the string. I don't know the exact code, but it would look something like this:
Code:
Function ChrIsAllowed
Const AllowedCharacters as String = "-.0123456789"
'This is the part that I'm not sure about, but it should be close
Return InStr(Chr(PressedKey),AllowedCharacters)
End Function
Of course you could then test to make sure that the resultant string is a number and not something like "04.4-697.-2"
-
May 13th, 2002, 08:36 AM
#5
Thread Starter
Hyperactive Member
Just for info, this is the function I created to validate it. As it happens I don't need to be able to enter negative values so I haven't added code for that.
I could also perhaps have an overriding class that uses a string and not a textbox, however the reason I did it with a textbox was because I will need to add code to incorporate the selstart and sellength properties...
However - for now.
To Use...
VB Code:
Private Sub txtAmount_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtAmount.KeyPress
If Not Common.Utilities.cUtils.isValidDecimal(txtAmount, e, 3) Then
Beep()
e.Handled = True
End If
End Sub
VB Code:
Shared Function isValidDecimal(ByVal txtBox As System.Windows.Forms.TextBox, _
ByVal e As System.Windows.Forms.KeyPressEventArgs, _
Optional ByVal intDecPlace As Int32 = -1) As Boolean
'**************************************************
'* Is Numeric Only (including decimal) *
'**************************************************
If Not e.KeyChar = ControlChars.Back Then
If Not e.KeyChar.IsDigit(e.KeyChar) Then
If e.KeyChar <> "." Then
Return False
Else
'***************************************
'* Only allow one decimal point *
'***************************************
If txtBox.Text.IndexOf("."c) > -1 Then
Return False
End If
End If
Else
'***************************************
'* Only allow specified decimal places *
'***************************************
If intDecPlace > 1 Then
Dim intX As Int32 = txtBox.Text.IndexOf("."c)
If intX > -1 Then
If txtBox.Text.Length - intX > intDecPlace Then
Return False
End If
End If
End If
End If
End If
Return True
End Function
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|