Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Validate Textbox only to accept numeric

Validate Textbox only to accept numeric

Scheduled Pinned Locked Moved C#
csharphelptutorial
7 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    aaraaayen 0
    wrote on last edited by
    #1

    Hi friends, how to validate textbox should accept only numeric in c#.Net. Please Help. Thanks in Advance, Regards,

    Prya

    E M D E 4 Replies Last reply
    0
    • A aaraaayen 0

      Hi friends, how to validate textbox should accept only numeric in c#.Net. Please Help. Thanks in Advance, Regards,

      Prya

      E Offline
      E Offline
      Ed Poore
      wrote on last edited by
      #2

      You can override the OnKeyPress or OnValidating methods of the TextBox, if overriding the KeyPress then simply filter out the keystrokes which are not numeric.  If overriding the OnValidating method then you can use int.TryParse to check to see if it's a valid integer. There are numerous articles on the web about how to do this, the best solution in my opinion would be to override the OnKeyPress event.


      I have no idea what I just said but my intentions were sincere. Poore Design

      Q 1 Reply Last reply
      0
      • E Ed Poore

        You can override the OnKeyPress or OnValidating methods of the TextBox, if overriding the KeyPress then simply filter out the keystrokes which are not numeric.  If overriding the OnValidating method then you can use int.TryParse to check to see if it's a valid integer. There are numerous articles on the web about how to do this, the best solution in my opinion would be to override the OnKeyPress event.


        I have no idea what I just said but my intentions were sincere. Poore Design

        Q Offline
        Q Offline
        quiteSmart
        wrote on last edited by
        #3

        That will work if the user didn't use the paste feature

        E 1 Reply Last reply
        0
        • A aaraaayen 0

          Hi friends, how to validate textbox should accept only numeric in c#.Net. Please Help. Thanks in Advance, Regards,

          Prya

          M Offline
          M Offline
          Martin 0
          wrote on last edited by
          #4

          Hello, Look at this articles here on CP http://www.codeproject.com/info/search.asp?cats=3&searchkw=TextBox+Numeric&Submit1=Search&author=&sd=15+Nov+1999&ed=24+Jan+2007[^] All the best, Martin

          1 Reply Last reply
          0
          • A aaraaayen 0

            Hi friends, how to validate textbox should accept only numeric in c#.Net. Please Help. Thanks in Advance, Regards,

            Prya

            D Offline
            D Offline
            David Domingues
            wrote on last edited by
            #5

            to complete with a bit of code, what has been already so nicely said:

            void TxtRegisteringKeyKeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
            {
            char[] myNum = new char[11] { '0' , '1', '2', '3', '4', '5', '6', '7', '8', '9', '\b' };
            bool blnNumFnd = false;
            foreach (char c in myNum)
            {
            if (e.KeyChar == c)
            blnNumFnd = true;
            }
            if (!blnNumFnd)
            e.KeyChar = '\0';
            }

            Contact me! Please feel free to visit my site

            1 Reply Last reply
            0
            • Q quiteSmart

              That will work if the user didn't use the paste feature

              E Offline
              E Offline
              Ed Poore
              wrote on last edited by
              #6

              Hadn't thought of that :doh:, suppose it'll have to be either the OnValidating or OnTextChanged.


              I have no idea what I just said but my intentions were sincere. Poore Design

              1 Reply Last reply
              0
              • A aaraaayen 0

                Hi friends, how to validate textbox should accept only numeric in c#.Net. Please Help. Thanks in Advance, Regards,

                Prya

                E Offline
                E Offline
                engsrini
                wrote on last edited by
                #7

                Use MaskedTextBox Instead of Normal textbox, where you can restrict user entries

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups