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. Web Development
  3. ASP.NET
  4. Restricting text entry in a textbox to numericals only

Restricting text entry in a textbox to numericals only

Scheduled Pinned Locked Moved ASP.NET
helpquestion
8 Posts 4 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.
  • Z Offline
    Z Offline
    ZimCoder
    wrote on last edited by
    #1

    Hi I have a textbox on my webform. I want to restrict the data entered into the textbox to numericals only. When someon types into the textbox they must not be able to type any other character except numbers. Is it possible .. Please help with sample code Thanks zimcoder What Democracy?? Jesus Christ is King and if you do not like... well you can go to hell!

    M T 2 Replies Last reply
    0
    • Z ZimCoder

      Hi I have a textbox on my webform. I want to restrict the data entered into the textbox to numericals only. When someon types into the textbox they must not be able to type any other character except numbers. Is it possible .. Please help with sample code Thanks zimcoder What Democracy?? Jesus Christ is King and if you do not like... well you can go to hell!

      M Offline
      M Offline
      Murugavel S
      wrote on last edited by
      #2

      Add the following javascript function to your .aspx page. In the textbox control add onkeypress="return Textbox1KeyPress(event)" in .aspx file. function Textbox1KeyPress(event) { var Decimal = 46 var DeciPlaces = 4 var AllowNegative = false var myString = new String(event.srcElement.value); var pntPos = myString.indexOf(String.fromCharCode(Decimal)); var keyChar = window.event.keyCode; if ((keyChar < 48) || (keyChar > 57)) { if (keyChar == Decimal) { if ((pntPos != -1) || (DeciPlaces < 1)) { return false; } return true; } else if (((keyChar == 45) && (!AllowNegative || myString.length != 0)) || (keyChar != 45)) {return false;} } return true; } This is a multipurpose function and will check for decimal places and negative values. Set the maxlength attribute to limit the input values for the textbox. Cheers;) </x-turndown>

      Z 1 Reply Last reply
      0
      • M Murugavel S

        Add the following javascript function to your .aspx page. In the textbox control add onkeypress="return Textbox1KeyPress(event)" in .aspx file. function Textbox1KeyPress(event) { var Decimal = 46 var DeciPlaces = 4 var AllowNegative = false var myString = new String(event.srcElement.value); var pntPos = myString.indexOf(String.fromCharCode(Decimal)); var keyChar = window.event.keyCode; if ((keyChar < 48) || (keyChar > 57)) { if (keyChar == Decimal) { if ((pntPos != -1) || (DeciPlaces < 1)) { return false; } return true; } else if (((keyChar == 45) && (!AllowNegative || myString.length != 0)) || (keyChar != 45)) {return false;} } return true; } This is a multipurpose function and will check for decimal places and negative values. Set the maxlength attribute to limit the input values for the textbox. Cheers;) </x-turndown>

        Z Offline
        Z Offline
        ZimCoder
        wrote on last edited by
        #3

        Thanks So much Muru;)gavel S!!! zimcoder What Democracy?? Jesus Christ is King and if you do not like... well you can go to hell!

        1 Reply Last reply
        0
        • Z ZimCoder

          Hi I have a textbox on my webform. I want to restrict the data entered into the textbox to numericals only. When someon types into the textbox they must not be able to type any other character except numbers. Is it possible .. Please help with sample code Thanks zimcoder What Democracy?? Jesus Christ is King and if you do not like... well you can go to hell!

          T Offline
          T Offline
          Tuwing Sabado
          wrote on last edited by
          #4

          Public Const strNumeric_OnKeypress_WO_Comma_and_Period As String = "javascript:if (event.keyCode < 48 || event.keyCode > 57) event.returnValue = false;" Public Const strNumeric_OnKeypress_W_Comma_and_Period As String = "javascript:if ((event.keyCode < 48 && event.keyCode != 46 && event.keyCode != 44) || event.keyCode > 57) event.returnValue = false;" Public Const strNumeric_OnKeypress_W_Period As String = "javascript:if ((event.keyCode < 48 && event.keyCode != 46) || event.keyCode > 57) event.returnValue = false;" Add the three constant variable in code behind and use the attribute of textbox control like this. txtNoOfDeposit.Attributes("onkeypress") = strNumeric_OnKeypress_WO_Comma_and_Period

          E 1 Reply Last reply
          0
          • T Tuwing Sabado

            Public Const strNumeric_OnKeypress_WO_Comma_and_Period As String = "javascript:if (event.keyCode < 48 || event.keyCode > 57) event.returnValue = false;" Public Const strNumeric_OnKeypress_W_Comma_and_Period As String = "javascript:if ((event.keyCode < 48 && event.keyCode != 46 && event.keyCode != 44) || event.keyCode > 57) event.returnValue = false;" Public Const strNumeric_OnKeypress_W_Period As String = "javascript:if ((event.keyCode < 48 && event.keyCode != 46) || event.keyCode > 57) event.returnValue = false;" Add the three constant variable in code behind and use the attribute of textbox control like this. txtNoOfDeposit.Attributes("onkeypress") = strNumeric_OnKeypress_WO_Comma_and_Period

            E Offline
            E Offline
            enjoycrack
            wrote on last edited by
            #5

            Hi there, This solution just prevent user stroke, but cannot prevent user drag & drop restricted text from another. Now I still have not found the solution for this Could u help me to solve this? thanks in advance :((:-O

            T 2 Replies Last reply
            0
            • E enjoycrack

              Hi there, This solution just prevent user stroke, but cannot prevent user drag & drop restricted text from another. Now I still have not found the solution for this Could u help me to solve this? thanks in advance :((:-O

              T Offline
              T Offline
              Tuwing Sabado
              wrote on last edited by
              #6

              Hi, Sorry for my late reply. You can not prevent the user to Paste or drag and drop some text but you can add additional validation in "onblur" attributes like checking if the text is numeric or in a right format. ex. Public Const strNumeric_OnKeypress_WO_Comma_and_Period As String = _ "javascript:if (event.keyCode < 48 || event.keyCode > 57) event.returnValue = false;" Public Const strNumeric_OnKeypress_W_Comma_and_Period As String = _ "javascript:if ((event.keyCode < 48 && event.keyCode != 46 && event.keyCode != 44)" & _ "|| event.keyCode > 57) event.returnValue = false;" Public Const strNumeric_OnKeypress_W_Period As String = _ "javascript:if ((event.keyCode < 48 && event.keyCode != 46)" & _ "|| event.keyCode > 57) event.returnValue = false;" Public Const strNumeric_OnBlur = "javascript:if(isNaN(this.value)==true) " & _ {window.alert("invalid numeric input!");this.value=''}" '~~~ Add the 4 constant variable in code behind and use the attribute '~~~ of textbox control like this. '~~~ Validation on keypress txtNoOfDeposit.Attributes("onkeypress") = strNumeric_OnKeypress_WO_Comma_and_Period '~~~ Validation on OnBlur or Lost of Focus txtNoOfDeposit.Attributes("onblur") = strNumeric_OnBlur Happy Coding.... Mark

              E 1 Reply Last reply
              0
              • E enjoycrack

                Hi there, This solution just prevent user stroke, but cannot prevent user drag & drop restricted text from another. Now I still have not found the solution for this Could u help me to solve this? thanks in advance :((:-O

                T Offline
                T Offline
                Tuwing Sabado
                wrote on last edited by
                #7

                Please Replace javascriptif by javascript::if

                1 Reply Last reply
                0
                • T Tuwing Sabado

                  Hi, Sorry for my late reply. You can not prevent the user to Paste or drag and drop some text but you can add additional validation in "onblur" attributes like checking if the text is numeric or in a right format. ex. Public Const strNumeric_OnKeypress_WO_Comma_and_Period As String = _ "javascript:if (event.keyCode < 48 || event.keyCode > 57) event.returnValue = false;" Public Const strNumeric_OnKeypress_W_Comma_and_Period As String = _ "javascript:if ((event.keyCode < 48 && event.keyCode != 46 && event.keyCode != 44)" & _ "|| event.keyCode > 57) event.returnValue = false;" Public Const strNumeric_OnKeypress_W_Period As String = _ "javascript:if ((event.keyCode < 48 && event.keyCode != 46)" & _ "|| event.keyCode > 57) event.returnValue = false;" Public Const strNumeric_OnBlur = "javascript:if(isNaN(this.value)==true) " & _ {window.alert("invalid numeric input!");this.value=''}" '~~~ Add the 4 constant variable in code behind and use the attribute '~~~ of textbox control like this. '~~~ Validation on keypress txtNoOfDeposit.Attributes("onkeypress") = strNumeric_OnKeypress_WO_Comma_and_Period '~~~ Validation on OnBlur or Lost of Focus txtNoOfDeposit.Attributes("onblur") = strNumeric_OnBlur Happy Coding.... Mark

                  E Offline
                  E Offline
                  enjoycrack
                  wrote on last edited by
                  #8

                  hi there, we can prevent user to Paste some text by checking the event OnPaste of textbox, but I dont known how to prevent user to Drag & drop :((:-O

                  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