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. Validation to accept only numbers!!!

Validation to accept only numbers!!!

Scheduled Pinned Locked Moved ASP.NET
questioncsharpasp-net
12 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.
  • R Offline
    R Offline
    rameshbhojan
    wrote on last edited by
    #1

    Hi all, Is there any specific property or control in ASP.NET 2.0 using which i can enter only numerical characters in a textbox?? How do i do this??? If no, please semd me the piece of code using which i can do this validation. Thanks Ramesh

    R S R P M 5 Replies Last reply
    0
    • R rameshbhojan

      Hi all, Is there any specific property or control in ASP.NET 2.0 using which i can enter only numerical characters in a textbox?? How do i do this??? If no, please semd me the piece of code using which i can do this validation. Thanks Ramesh

      R Offline
      R Offline
      ritu4321
      wrote on last edited by
      #2

      //here formElement is the control and myString is the string typed into it . //By using this javascripting code the characters and others will not //get //typed only numbers will type. function removeInvalidChars(formElement, myString) { var intPeriod = 0 for (var i = 0; i < myString.length; i++) { A_char = myString.charCodeAt(i) //We are validating for numbers. if (A_char > 47 && A_char < 58) { // It's a number continue; } else { myString = myString.substring(0,i) } } formElement.value = myString; }

      1 Reply Last reply
      0
      • R rameshbhojan

        Hi all, Is there any specific property or control in ASP.NET 2.0 using which i can enter only numerical characters in a textbox?? How do i do this??? If no, please semd me the piece of code using which i can do this validation. Thanks Ramesh

        R Offline
        R Offline
        Raghvendra Kumar Roy
        wrote on last edited by
        #3

        you can use regular expression validation like this: ""asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Numeric Value" ControlToValidate="TextBox1" ValidationExpression="[0-9]">Raghvendra Kumar Roy

        R 2 Replies Last reply
        0
        • R rameshbhojan

          Hi all, Is there any specific property or control in ASP.NET 2.0 using which i can enter only numerical characters in a textbox?? How do i do this??? If no, please semd me the piece of code using which i can do this validation. Thanks Ramesh

          S Offline
          S Offline
          Sathesh Sakthivel
          wrote on last edited by
          #4

          This script is used for u accept only numbers. function isinteg(obj){ if (isNaN(obj.value)){ obj.value=""; return false; } return true; }

          Regards, Satips.:rose:

          1 Reply Last reply
          0
          • R rameshbhojan

            Hi all, Is there any specific property or control in ASP.NET 2.0 using which i can enter only numerical characters in a textbox?? How do i do this??? If no, please semd me the piece of code using which i can do this validation. Thanks Ramesh

            P Offline
            P Offline
            Puneet Sri
            wrote on last edited by
            #5

            function intOnly(obj) { if(obj.value!=parseInt(obj.value)) { alert("Please Enter Whole Number only"); obj.value=""; } } i hope this will help you. Puneet Srivastava

            1 Reply Last reply
            0
            • R Raghvendra Kumar Roy

              you can use regular expression validation like this: ""asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Numeric Value" ControlToValidate="TextBox1" ValidationExpression="[0-9]">Raghvendra Kumar Roy

              R Offline
              R Offline
              rameshbhojan
              wrote on last edited by
              #6

              Hi Raghav, Thanks! n wat do i do if i want to validate a filed called year which should accept only 4 digit year like 2007 and not 07. also i want a textbox which should not accept special characters.

              R 1 Reply Last reply
              0
              • R rameshbhojan

                Hi Raghav, Thanks! n wat do i do if i want to validate a filed called year which should accept only 4 digit year like 2007 and not 07. also i want a textbox which should not accept special characters.

                R Offline
                R Offline
                Raghvendra Kumar Roy
                wrote on last edited by
                #7

                if you want to validate a filed called year which should accept only 4 digit year like 2007 and not 07. try this: After validationexpression you can define numbers Just add this for your problem ValidationExpression="[0-9]{4}" if you want a textbox which should not accept special characters. it will accept only uppercase characters,lowercase characters and numeric characters. ValidationExpression="[A-Z,a-z][0-9]" Thanks

                Raghvendra Kumar Roy

                1 Reply Last reply
                0
                • R rameshbhojan

                  Hi all, Is there any specific property or control in ASP.NET 2.0 using which i can enter only numerical characters in a textbox?? How do i do this??? If no, please semd me the piece of code using which i can do this validation. Thanks Ramesh

                  M Offline
                  M Offline
                  mnarayana3
                  wrote on last edited by
                  #8

                  Public Class Form2 Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress Dim c As Integer c = Asc(e.KeyChar) If c = 8 Or c = 13 Then Exit Sub ElseIf c < 47 Or c > 57 Then e.Handled = True ErrorProvider1.SetError(TextBox1, "Invalid Number") Else e.Handled = False ErrorProvider1.SetError(TextBox1, "") End If End Sub End Class just create a user control and use it wherever u want

                  1 Reply Last reply
                  0
                  • R Raghvendra Kumar Roy

                    you can use regular expression validation like this: ""asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Numeric Value" ControlToValidate="TextBox1" ValidationExpression="[0-9]">Raghvendra Kumar Roy

                    R Offline
                    R Offline
                    rameshbhojan
                    wrote on last edited by
                    #9

                    hey raghav...... this accepts only a single digit number!!!! i want the field such tat it should accept number..... any number of any length..... but this validation coontrol accepts only a single digit no. not any other number also please help asap. thanks Ramesh

                    R 1 Reply Last reply
                    0
                    • R rameshbhojan

                      hey raghav...... this accepts only a single digit number!!!! i want the field such tat it should accept number..... any number of any length..... but this validation coontrol accepts only a single digit no. not any other number also please help asap. thanks Ramesh

                      R Offline
                      R Offline
                      Raghvendra Kumar Roy
                      wrote on last edited by
                      #10

                      Hey Ramesh Try this: ValidationExpression="[0-9]*$"

                      Raghvendra Kumar Roy

                      R 1 Reply Last reply
                      0
                      • R Raghvendra Kumar Roy

                        Hey Ramesh Try this: ValidationExpression="[0-9]*$"

                        Raghvendra Kumar Roy

                        R Offline
                        R Offline
                        rameshbhojan
                        wrote on last edited by
                        #11

                        Hey Raghav, I have been able to do it by simply adding a star ValidationExpression="[0-9]*" For wat is tat additional $ ? Please let me know

                        R 1 Reply Last reply
                        0
                        • R rameshbhojan

                          Hey Raghav, I have been able to do it by simply adding a star ValidationExpression="[0-9]*" For wat is tat additional $ ? Please let me know

                          R Offline
                          R Offline
                          Raghvendra Kumar Roy
                          wrote on last edited by
                          #12

                          hi Ramesh $ means stop looking at this position.

                          Raghvendra Kumar Roy

                          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