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. Visual Basic
  4. How to add only numeric values to a textbox control

How to add only numeric values to a textbox control

Scheduled Pinned Locked Moved Visual Basic
helptutorial
7 Posts 7 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.
  • N Offline
    N Offline
    nagalakshmibasireddy
    wrote on last edited by
    #1

    Hello EveryOne, How to allow only numeric values ina textbox.if we allow characters it should pop up a message showing "need to enter numeric values". Please HElp Regarding this, Thanks Nagalakshmi

    H N J S C 5 Replies Last reply
    0
    • N nagalakshmibasireddy

      Hello EveryOne, How to allow only numeric values ina textbox.if we allow characters it should pop up a message showing "need to enter numeric values". Please HElp Regarding this, Thanks Nagalakshmi

      H Offline
      H Offline
      hertz_j
      wrote on last edited by
      #2

      Hi, I don't know if this is the best way of doing it, but you could capture the textbox on change event and then loop through each char in the text field to check if it is a number or not. So something like this: Public Sub OnTextChange(Byval sender as Object, Byval e EventArgs) Handels T.TextChanged for each c as Char in T.Text.Chars if Not c.IsNumber Then MessageBox.Show("Bla bla bla") end if Next End Sub Hope this helps /Johan

      1 Reply Last reply
      0
      • N nagalakshmibasireddy

        Hello EveryOne, How to allow only numeric values ina textbox.if we allow characters it should pop up a message showing "need to enter numeric values". Please HElp Regarding this, Thanks Nagalakshmi

        N Offline
        N Offline
        noshaba mariam
        wrote on last edited by
        #3

        u can also do this on keypress event of text box Private Sub txt1_KeyPress(KeyAscii As Integer) dim strValid as string strValid = "1234567890" if instr(strValid , chr(keyAscii)) = 0 then keyAscii = 0 end if end sub It would help u not to enter any character other than numeric values Noshaba

        1 Reply Last reply
        0
        • N nagalakshmibasireddy

          Hello EveryOne, How to allow only numeric values ina textbox.if we allow characters it should pop up a message showing "need to enter numeric values". Please HElp Regarding this, Thanks Nagalakshmi

          J Offline
          J Offline
          J4amieC
          wrote on last edited by
          #4

          Rather than annoy the user with a popup message which theyneed to discard every time they type an incorrect character, you should really restrict the characters that can be entered in the first place. There are plenty of articles/posts on this subject here at CP if you search.

          J 1 Reply Last reply
          0
          • N nagalakshmibasireddy

            Hello EveryOne, How to allow only numeric values ina textbox.if we allow characters it should pop up a message showing "need to enter numeric values". Please HElp Regarding this, Thanks Nagalakshmi

            S Offline
            S Offline
            sathish s
            wrote on last edited by
            #5

            http://www.cambiaresearch.com/cambia3/snippets/javascript/browser/allownumbersonly.aspx

            1 Reply Last reply
            0
            • N nagalakshmibasireddy

              Hello EveryOne, How to allow only numeric values ina textbox.if we allow characters it should pop up a message showing "need to enter numeric values". Please HElp Regarding this, Thanks Nagalakshmi

              C Offline
              C Offline
              Chatura Dilan
              wrote on last edited by
              #6

              One of easy ways....:) Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress If (AscW(e.KeyChar) < 48 Or AscW(e.KeyChar) > 57) And Not e.KeyChar = Chr(Keys.Back) Then e.KeyChar = Nothing End If End Sub

              Regards,

              Chatura Dilan

              1 Reply Last reply
              0
              • J J4amieC

                Rather than annoy the user with a popup message which theyneed to discard every time they type an incorrect character, you should really restrict the characters that can be entered in the first place. There are plenty of articles/posts on this subject here at CP if you search.

                J Offline
                J Offline
                Jim from Indy
                wrote on last edited by
                #7

                I built a little function: function CashVal (byRef Intext as object, byRef ErrInd as boolean) as double 1. Check Intext to be sure it's not nothing 2. Check Intext to be sure it's not null 3. Check Intext to be sure it's numeric (and in my case, > 0...) 4. Return CDbl(Intext) and an appropriate ErrInd. I invoke this (x = CashVal(textboxname.text,Error_Indicator) )when the "leave" event is fired, so the user can type anything, and it's evaluated as a whole. The event handler checks to see if there was an error. If not, it sets the text property of the text box to format (x, "currency"). If there's an error, obviously, it can react appropriately.

                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