How to Validate the Data while entering Data into textbox only for numeric values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • robintwos
    New Member
    • Nov 2007
    • 24

    How to Validate the Data while entering Data into textbox only for numeric values

    Hi

    I would like to validate the while entering the data in a textbox for numeric values.
    it means . i would like to throw an error message when the user enters a character value. validation should happen while entering it self .

    I am looking for the piece of code in VB.NET. Please do help me

    Thanks
  • dip_developer
    Recognized Expert Contributor
    • Aug 2006
    • 648

    #2
    Originally posted by robintwos
    Hi

    I would like to validate the while entering the data in a textbox for numeric values.
    it means . i would like to throw an error message when the user enters a character value. validation should happen while entering it self .

    I am looking for the piece of code in VB.NET. Please do help me

    Thanks
    just write a function like the following

    [CODE=vbnet]
    Private Function checkForNumeric Input(ByVal e As System.Windows. Forms.KeyPressE ventArgs)
    If Not (Char.IsDigit(e .KeyChar) Or Char.IsControl( e.KeyChar) ) Then
    e.Handled = True
    MsgBox("Please Input Numbers Only", MsgBoxStyle.Cri tical)
    End If
    End Function
    [/CODE]

    Comment

    • robintwos
      New Member
      • Nov 2007
      • 24

      #3
      Hi Thanks for ur code .. I can enter "." also . so i modified the code to allow "." But now i am able to enter "." multiple times .. its an amount field and "." should be entered only once.

      Comment

      • debasisdas
        Recognized Expert Expert
        • Dec 2006
        • 8119

        #4
        Originally posted by robintwos
        Hi Thanks for ur code .. I can enter "." also . so i modified the code to allow "." But now i am able to enter "." multiple times .. its an amount field and "." should be entered only once.
        Try to solve that yourself refering the sample provided by our expert in the previous post.

        Comment

        • dip_developer
          Recognized Expert Contributor
          • Aug 2006
          • 648

          #5
          rightly said by debasisdas...pl ease try it yourself for your betterment..... .i hope you can do....its fairly easy to validate multiple dots....put your brain....

          Comment

          • robintwos
            New Member
            • Nov 2007
            • 24

            #6
            Thanks for the replies . I solved it my self . I publish my questions here as i started learning .NET newly. Sorry if you people think , i am asking very much basic doubts. I'm learning it on my own. i dont know much in VB.NET.

            Comment

            • hellogohel
              New Member
              • Nov 2007
              • 6

              #7
              Private Sub MyTextbox_KeyPr ess(ByVal sender As Object, ByVal e As System.Windows. Forms.KeyPressE ventArgs) Handles MyBase.KeyPress
              If Char.IsDigit(e. KeyChar) = True Or e.KeyChar = Chr(Keys.Back) Then
              Exit Sub
              Else
              e.Handled = True
              MessageBox.Show ("PLEASE ENTER ONLY NUMERIC VALUE.")
              End If
              End Sub

              Comment

              Working...