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. Web Development
  3. ASP.NET
  4. number field

number field

Scheduled Pinned Locked Moved ASP.NET
question
10 Posts 5 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.
  • L Offline
    L Offline
    lhahehal
    wrote on last edited by
    #1

    hi there is there a way to make the field accept numbers only ??

    L D 2 Replies Last reply
    0
    • L lhahehal

      hi there is there a way to make the field accept numbers only ??

      L Offline
      L Offline
      liuchen
      wrote on last edited by
      #2

      u can use JavaScript to check ! Also if u code with asp.net ,u can use a 'Web Control' named 'Validators' do this! Hope it's useful to u! liuchen from China

      1 Reply Last reply
      0
      • L lhahehal

        hi there is there a way to make the field accept numbers only ??

        D Offline
        D Offline
        DavidNohejl
        wrote on last edited by
        #3

        hi, I suppose you mean textbox... yes, I think you can do that with JS filtering onKeyDown (or whicho one it is) event. One more solution is to use ASP.NET's [RegularExpressionValidator](http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebuiwebcontrolsregularexpressionvalidatorclasstopic.asp)[[^](http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebuiwebcontrolsregularexpressionvalidatorclasstopic.asp "New Window")], if you don't mind that user can insert non-numbers there, but after try to submit, it won't submit but show error message to user. If it matters for security reasons, you HAVE TO do server-side check as well, because malicious user (aka hacker) don't have problem to bypass JS validation. it's as easy as disabling JS in browser, btw. David Never forget: "Stay kul and happy" (I.A.)

        L P G 3 Replies Last reply
        0
        • D DavidNohejl

          hi, I suppose you mean textbox... yes, I think you can do that with JS filtering onKeyDown (or whicho one it is) event. One more solution is to use ASP.NET's [RegularExpressionValidator](http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebuiwebcontrolsregularexpressionvalidatorclasstopic.asp)[[^](http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebuiwebcontrolsregularexpressionvalidatorclasstopic.asp "New Window")], if you don't mind that user can insert non-numbers there, but after try to submit, it won't submit but show error message to user. If it matters for security reasons, you HAVE TO do server-side check as well, because malicious user (aka hacker) don't have problem to bypass JS validation. it's as easy as disabling JS in browser, btw. David Never forget: "Stay kul and happy" (I.A.)

          P Offline
          P Offline
          powerlc
          wrote on last edited by
          #4

          Yes, i think ASP.NET's RegularExpressionValidator is the best choose,it's a new feature in ASP.Net.

          1 Reply Last reply
          0
          • D DavidNohejl

            hi, I suppose you mean textbox... yes, I think you can do that with JS filtering onKeyDown (or whicho one it is) event. One more solution is to use ASP.NET's [RegularExpressionValidator](http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebuiwebcontrolsregularexpressionvalidatorclasstopic.asp)[[^](http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebuiwebcontrolsregularexpressionvalidatorclasstopic.asp "New Window")], if you don't mind that user can insert non-numbers there, but after try to submit, it won't submit but show error message to user. If it matters for security reasons, you HAVE TO do server-side check as well, because malicious user (aka hacker) don't have problem to bypass JS validation. it's as easy as disabling JS in browser, btw. David Never forget: "Stay kul and happy" (I.A.)

            L Offline
            L Offline
            lhahehal
            wrote on last edited by
            #5

            hmm lol , then i think i can strike out JS ... btw the regular expression validator has no default number field validator ...

            D 1 Reply Last reply
            0
            • L lhahehal

              hmm lol , then i think i can strike out JS ... btw the regular expression validator has no default number field validator ...

              D Offline
              D Offline
              DavidNohejl
              wrote on last edited by
              #6

              lhahehal wrote: then i think i can strike out JS No, use JS validation for usability... when user accidentaly type in wrong input, you display warning without unnecesary postback. Best is to keep both client and server-side validation. lhahehal wrote: regular expression validator has no default number field validator No problem here, I think you can type custom regex here.. for numbers (ehm, for arabic numbers :) ) it's easy:

              [0-9]*
              

              Thats it. If you don't mind leading zeros.. if you don't want to allow leading zeros, try something like:^[1-9][0-9]*
              or maybe you want to allow +- at the beginning, decimal point/comma whatever.. see MSDN[^] or google for it... btw, here[^] is online Regex tester.. useful :cool: They have nice library as well. David Never forget: "Stay kul and happy" (I.A.)

              L 1 Reply Last reply
              0
              • D DavidNohejl

                lhahehal wrote: then i think i can strike out JS No, use JS validation for usability... when user accidentaly type in wrong input, you display warning without unnecesary postback. Best is to keep both client and server-side validation. lhahehal wrote: regular expression validator has no default number field validator No problem here, I think you can type custom regex here.. for numbers (ehm, for arabic numbers :) ) it's easy:

                [0-9]*
                

                Thats it. If you don't mind leading zeros.. if you don't want to allow leading zeros, try something like:^[1-9][0-9]*
                or maybe you want to allow +- at the beginning, decimal point/comma whatever.. see MSDN[^] or google for it... btw, here[^] is online Regex tester.. useful :cool: They have nice library as well. David Never forget: "Stay kul and happy" (I.A.)

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

                thanks it work very nicely :) btw can it be used for code behind , like textbox1.text != [0-9] ??

                P D 2 Replies Last reply
                0
                • L lhahehal

                  thanks it work very nicely :) btw can it be used for code behind , like textbox1.text != [0-9] ??

                  P Offline
                  P Offline
                  powerlc
                  wrote on last edited by
                  #8

                  sure ,you can use the "web control " code behind,you can see it in MSDN .

                  1 Reply Last reply
                  0
                  • L lhahehal

                    thanks it work very nicely :) btw can it be used for code behind , like textbox1.text != [0-9] ??

                    D Offline
                    D Offline
                    DavidNohejl
                    wrote on last edited by
                    #9

                    lhahehal wrote: btw can it be used for code behind , like textbox1.text != [0-9] ?? Not with this syntax, you need Regex[^] class. Read about it, there are examples... [edit]I see, you can use validator in code-behind, example is in MSDN again (follow link in my very first post)[/edit] David Never forget: "Stay kul and happy" (I.A.)

                    1 Reply Last reply
                    0
                    • D DavidNohejl

                      hi, I suppose you mean textbox... yes, I think you can do that with JS filtering onKeyDown (or whicho one it is) event. One more solution is to use ASP.NET's [RegularExpressionValidator](http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebuiwebcontrolsregularexpressionvalidatorclasstopic.asp)[[^](http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebuiwebcontrolsregularexpressionvalidatorclasstopic.asp "New Window")], if you don't mind that user can insert non-numbers there, but after try to submit, it won't submit but show error message to user. If it matters for security reasons, you HAVE TO do server-side check as well, because malicious user (aka hacker) don't have problem to bypass JS validation. it's as easy as disabling JS in browser, btw. David Never forget: "Stay kul and happy" (I.A.)

                      G Offline
                      G Offline
                      gnjunge
                      wrote on last edited by
                      #10

                      Why don't use the Validator that is really meant for this purpose: the CompareValidator[^], set type to Integer, and ControlToValidate to your Control, and that's it. This will check if the field is an integer. Gidon

                      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