Results 1 to 5 of 5

Thread: Validating Numerics

  1. #1

    Thread Starter
    Hyperactive Member Bananafish's Avatar
    Join Date
    Jan 2001
    Posts
    394

    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

  2. #2
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    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.

  3. #3

    Thread Starter
    Hyperactive Member Bananafish's Avatar
    Join Date
    Jan 2001
    Posts
    394
    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).

  4. #4
    Member
    Join Date
    Mar 2002
    Posts
    40
    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"

  5. #5

    Thread Starter
    Hyperactive Member Bananafish's Avatar
    Join Date
    Jan 2001
    Posts
    394
    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:
    1. Private Sub txtAmount_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtAmount.KeyPress
    2.  
    3.       If Not Common.Utilities.cUtils.isValidDecimal(txtAmount, e, 3) Then
    4.          Beep()
    5.          e.Handled = True
    6.       End If
    7. End Sub

    VB Code:
    1. Shared Function isValidDecimal(ByVal txtBox As System.Windows.Forms.TextBox, _
    2.            ByVal e As System.Windows.Forms.KeyPressEventArgs, _
    3.            Optional ByVal intDecPlace As Int32 = -1) As Boolean
    4.  
    5.       '**************************************************
    6.       '* Is Numeric Only (including decimal)            *
    7.       '**************************************************
    8.       If Not e.KeyChar = ControlChars.Back Then
    9.          If Not e.KeyChar.IsDigit(e.KeyChar) Then
    10.             If e.KeyChar <> "." Then
    11.                Return False
    12.             Else
    13.                '***************************************
    14.                '* Only allow one decimal point        *
    15.                '***************************************
    16.                If txtBox.Text.IndexOf("."c) > -1 Then
    17.                   Return False
    18.                End If
    19.             End If
    20.          Else
    21.             '***************************************
    22.             '* Only allow specified decimal places *
    23.             '***************************************
    24.             If intDecPlace > 1 Then
    25.                Dim intX As Int32 = txtBox.Text.IndexOf("."c)
    26.                If intX > -1 Then
    27.                   If txtBox.Text.Length - intX > intDecPlace Then
    28.                      Return False
    29.                   End If
    30.                End If
    31.             End If
    32.          End If
    33.       End If
    34.       Return True
    35.  
    36.    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
  •  



Click Here to Expand Forum to Full Width