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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. How to Allow only numbers for TextBox

How to Allow only numbers for TextBox

Scheduled Pinned Locked Moved C#
helptutorial
14 Posts 9 Posters 1 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.
  • S Offline
    S Offline
    sacr83
    wrote on last edited by
    #1

    I wanna allow only number for my TextBox1.Its can only insert 7 digits.If I enter "23ddddd" on my TextBox1 i wanna give an error message for that.So please help me...........

    CheeN

    G H W L G 7 Replies Last reply
    0
    • S sacr83

      I wanna allow only number for my TextBox1.Its can only insert 7 digits.If I enter "23ddddd" on my TextBox1 i wanna give an error message for that.So please help me...........

      CheeN

      G Offline
      G Offline
      Giorgi Dalakishvili
      wrote on last edited by
      #2

      In the Keypress event handler check if the pressed key is digit or not. If not set handled to true. Don't forget backspace key.

      Giorgi Dalakishvili #region signature my articles #endregion

      S 1 Reply Last reply
      0
      • S sacr83

        I wanna allow only number for my TextBox1.Its can only insert 7 digits.If I enter "23ddddd" on my TextBox1 i wanna give an error message for that.So please help me...........

        CheeN

        H Offline
        H Offline
        Harvey Saayman
        wrote on last edited by
        #3

        hey use a maskedTextBox instead of a notmal textBox

        Harvey Saayman - South Africa Junior Developer .Net, C#, SQL

        you.suck = (you.passion != Programming)

        1 Reply Last reply
        0
        • G Giorgi Dalakishvili

          In the Keypress event handler check if the pressed key is digit or not. If not set handled to true. Don't forget backspace key.

          Giorgi Dalakishvili #region signature my articles #endregion

          S Offline
          S Offline
          sacr83
          wrote on last edited by
          #4

          Can i use regular expression or any validater?If yes how can i use that..? i tried Match MatchTelNum = Regex.Match(tel.Text, "[a-z&0-9]*"); Its wroks.But i had a problem coz if i entered "3333333" to that textBox1.It also giv an error message.If you can help me.. ReagrDzzz

          CheeN

          L 1 Reply Last reply
          0
          • S sacr83

            I wanna allow only number for my TextBox1.Its can only insert 7 digits.If I enter "23ddddd" on my TextBox1 i wanna give an error message for that.So please help me...........

            CheeN

            W Offline
            W Offline
            wasimsharp
            wrote on last edited by
            #5

            Cheen on Press key Event of textbox use this code if (Char.IsLetter(e.KeyChar)) { e.Handled; }

            wasim khan

            L 1 Reply Last reply
            0
            • S sacr83

              Can i use regular expression or any validater?If yes how can i use that..? i tried Match MatchTelNum = Regex.Match(tel.Text, "[a-z&0-9]*"); Its wroks.But i had a problem coz if i entered "3333333" to that textBox1.It also giv an error message.If you can help me.. ReagrDzzz

              CheeN

              L Offline
              L Offline
              leppie
              wrote on last edited by
              #6

              sacr83 wrote:

              Match MatchTelNum = Regex.Match(tel.Text, "[a-z&0-9]*"); Its wroks.

              No, it does not work... That will match letters and digits and '&'.

              xacc.ide - now with TabsToSpaces support
              IronScheme - 1.0 alpha 4a out now (29 May 2008)

              1 Reply Last reply
              0
              • W wasimsharp

                Cheen on Press key Event of textbox use this code if (Char.IsLetter(e.KeyChar)) { e.Handled; }

                wasim khan

                L Offline
                L Offline
                leppie
                wrote on last edited by
                #7

                wasimsharp wrote:

                if (Char.IsLetter(e.KeyChar)) { e.Handled; }

                1. That wont compile. 2. Are you checking for digits or letters?

                xacc.ide - now with TabsToSpaces support
                IronScheme - 1.0 alpha 4a out now (29 May 2008)

                W 1 Reply Last reply
                0
                • S sacr83

                  I wanna allow only number for my TextBox1.Its can only insert 7 digits.If I enter "23ddddd" on my TextBox1 i wanna give an error message for that.So please help me...........

                  CheeN

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  int.TryParse in the key event handler is your friend.

                  L 1 Reply Last reply
                  0
                  • L Lost User

                    int.TryParse in the key event handler is your friend.

                    L Offline
                    L Offline
                    leppie
                    wrote on last edited by
                    #9

                    Greeeg wrote:

                    int.TryParse in the key event handler is your friend.

                    Friends dont let friends overflow! What happens if the person enters 25 digits?

                    xacc.ide - now with TabsToSpaces support
                    IronScheme - 1.0 alpha 4a out now (29 May 2008)

                    L 1 Reply Last reply
                    0
                    • S sacr83

                      I wanna allow only number for my TextBox1.Its can only insert 7 digits.If I enter "23ddddd" on my TextBox1 i wanna give an error message for that.So please help me...........

                      CheeN

                      G Offline
                      G Offline
                      Gareth H
                      wrote on last edited by
                      #10

                      sacr83, If you only want to allow numbers, why not use a NumericUpDown control... MSDN[^]

                      Regards, Gareth. (FKA gareth111)

                      1 Reply Last reply
                      0
                      • L leppie

                        wasimsharp wrote:

                        if (Char.IsLetter(e.KeyChar)) { e.Handled; }

                        1. That wont compile. 2. Are you checking for digits or letters?

                        xacc.ide - now with TabsToSpaces support
                        IronScheme - 1.0 alpha 4a out now (29 May 2008)

                        W Offline
                        W Offline
                        wasimsharp
                        wrote on last edited by
                        #11

                        No if (!Char.IsDigit(e.KeyChar)) { e.Handled = true; }

                        wasim khan

                        1 Reply Last reply
                        0
                        • S sacr83

                          I wanna allow only number for my TextBox1.Its can only insert 7 digits.If I enter "23ddddd" on my TextBox1 i wanna give an error message for that.So please help me...........

                          CheeN

                          A Offline
                          A Offline
                          Abhijit Jana
                          wrote on last edited by
                          #12

                          Just use this private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { **if (!char.IsDigit(e.KeyChar)) { e.Handled = true; MessageBox.Show("Integer numbers only"); }** }

                          Best Regards ----------------- Abhijit Jana Check Out My Latest Article Java.NET : Integration of Java and .NET[^] "Success is Journey it's not a destination"

                          1 Reply Last reply
                          0
                          • L leppie

                            Greeeg wrote:

                            int.TryParse in the key event handler is your friend.

                            Friends dont let friends overflow! What happens if the person enters 25 digits?

                            xacc.ide - now with TabsToSpaces support
                            IronScheme - 1.0 alpha 4a out now (29 May 2008)

                            L Offline
                            L Offline
                            Lost User
                            wrote on last edited by
                            #13

                            Then I should take the time to read the question more precisely ;) A regex like \d{,7} for at most, or \d{7} for exactly 7 digits will do the job as well. But I'd use a NumericUpDown control for this task.

                            1 Reply Last reply
                            0
                            • S sacr83

                              I wanna allow only number for my TextBox1.Its can only insert 7 digits.If I enter "23ddddd" on my TextBox1 i wanna give an error message for that.So please help me...........

                              CheeN

                              M Offline
                              M Offline
                              Mbah Dhaim
                              wrote on last edited by
                              #14

                              if you want to get an error as you type you can use KeyDown events then get your last entered character from your textbox, if last character is not what you suggest to input throw an errror

                              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