JavaScript to check whether textbox contains only integer or numeric/number values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mathewgk80
    New Member
    • Sep 2007
    • 103

    JavaScript to check whether textbox contains only integer or numeric/number values

    Hi all,

    I would like to get the Javascript code to check the textbox contains only numeric values or numbers..

    I am using asp.net and c#.net..

    Please help me..

    Regards,
    Mathew
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    hi ...

    you may use javascript built in isNaN() method for that ... it returns true in case it is NotaNumber else you get a false ...

    kind regards

    Comment

    • mathewgk80
      New Member
      • Sep 2007
      • 103

      #3
      Originally posted by gits
      hi ...

      you may use javascript built in isNaN() method for that ... it returns true in case it is NotaNumber else you get a false ...

      kind regards
      Hi Gits,

      Can you please give an example for that???

      regards,
      Mathew

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        ok ... lets assume following example:

        Code:
        <html>
            <head>
            <script type="text/javascript">
                function check_field(id) {
                    var field = document.getElementById(id);
                        
                    if (isNaN(field.value)) {
                        alert('not a number');
                    }
                }
            </script>
            </head>
            <body>
            <form>
                <input type="text" id="t_field"/>
                <input type="button" value="check" onclick="check_field('t_field');"/>
            </form>
            </body>
        </html>
        kind regards

        Comment

        • ananth
          New Member
          • Nov 2006
          • 75

          #5
          HI ,
          The code above using NAN will not work properly try this.

          [CODE=javascript]function checkNum(x)
          {

          var s_len=x.value.l ength ;
          var s_charcode = 0;
          for (var s_i=0;s_i<s_len ;s_i++)
          {
          s_charcode = x.value.charCod eAt(s_i);
          if(!((s_charcod e>=48 && s_charcode<=57) ))
          {
          alert("Only Numeric Values Allowed");
          x.value='';
          x.focus();
          return false;
          }
          }
          return true;
          }

          <input type="text" id="t_field" onChange='check Num(this)'/>[/CODE]

          This will execute on the field exit or tabbing out of the field.

          Hope this would be useful.
          Last edited by gits; Oct 19 '07, 09:59 AM. Reason: added code tags

          Comment

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5390

            #6
            Originally posted by ananth
            HI ,
            The code above using NAN will not work properly try this.
            nope ... it is working properly for the requirement. it checks for numeric values and it works! you didn't say why or what is not working properly!

            your code should check for only Integers? you simply could use one line for that:

            [CODE=javascript]// in case value is an integer test_result will be true
            // otherwise it will be false
            var test_result = /^\d+$/.test(value);
            [/CODE]
            kind regards

            Comment

            • mathewgk80
              New Member
              • Sep 2007
              • 103

              #7
              check whether the textbox contains only integer or numeric values

              Hi all,

              I would like to check whether the textbox contains only nmumberic values by using javascript...

              Please help me...

              Regards,
              Mathew

              Comment

              • iam_clint
                Recognized Expert Top Contributor
                • Jul 2006
                • 1207

                #8
                I would use something similar to this and just call this function everytime you need to know if its numeric
                [CODE=javascript]
                <script>
                function isNumeric(val) {
                var numeric = true;
                var chars = "0123456789 .-,+";
                var len = val.length;
                var char = "";
                for (i=0; i<len; i++) { char = val.charAt(i); if (chars.indexOf( char)==-1) { numeric = false; } }
                return numeric;
                }
                </script>
                [/CODE]
                so I would say if (isNumeric("wha tever value here")) { alert("Its Numeric!!!"); }

                Comment

                • acoder
                  Recognized Expert MVP
                  • Nov 2006
                  • 16032

                  #9
                  Or use regular expressions:
                  [CODE=javascript]function isNumeric(str) {
                  return /^\d$/.match(str);
                  }[/CODE]

                  Comment

                  • iam_clint
                    Recognized Expert Top Contributor
                    • Jul 2006
                    • 1207

                    #10
                    Acoders way is better. Never even thought to do that :)

                    Comment

                    • acoder
                      Recognized Expert MVP
                      • Nov 2006
                      • 16032

                      #11
                      Actually, there's a typo (or rather I missed a +):
                      [CODE=javascript]function isNumeric(str) {
                      return /^\d+$/.match(str);
                      }[/CODE]

                      Comment

                      • acoder
                        Recognized Expert MVP
                        • Nov 2006
                        • 16032

                        #12
                        One more thing, this would only match positive integers, i.e. 0+.

                        If you want to match real numbers, you could try something like
                        Code:
                        /^(\+|-)?[1-9]\d*(\.\d*)?$/

                        Comment

                        • Kid Electric
                          New Member
                          • Nov 2007
                          • 1

                          #13
                          Originally posted by gits
                          nope ... it is working properly for the requirement. it checks for numeric values and it works! you didn't say why or what is not working properly!

                          your code should check for only Integers? you simply could use one line for that:

                          [CODE=javascript]// in case value is an integer test_result will be true
                          // otherwise it will be false
                          var test_result = /^\d+$/.test(value);
                          [/CODE]
                          kind regards

                          Hmmm... it's been a while since I've coded, but I'm unfamiliar with this syntax.

                          Unfortunately, I tried it and it didn't seem to work, whereas the previous (longer) solution did. This one-line code seemed to return a response of "false" even if a single integer was entered. (using IE 7.0.6)

                          Comment

                          • steve waugh
                            New Member
                            • Oct 2007
                            • 13

                            #14
                            Hi Mathew

                            Try this code. It Accepts only Numeric, u can modify it according to ur requirement.

                            Call the function onKeyPress event of textbox.

                            [CODE=javascript] function allownumber(e)
                            {

                            var key = window.event ? e.keyCode : e.which;
                            var keychar = String.fromChar Code(key);
                            var reg = new RegExp("[0-9.]")
                            if (key == 8)
                            {
                            keychar = String.fromChar Code(key);

                            }
                            if (key == 13)
                            {
                            key=8;
                            keychar = String.fromChar Code(key);
                            }
                            return reg.test(keycha r);
                            }[/CODE]

                            Thx

                            Originally posted by mathewgk80
                            Hi all,

                            I would like to get the Javascript code to check the textbox contains only numeric values or numbers..

                            I am using asp.net and c#.net..

                            Please help me..

                            Regards,
                            Mathew
                            Last edited by gits; Nov 12 '07, 08:05 AM. Reason: added code tags

                            Comment

                            • gits
                              Recognized Expert Moderator Expert
                              • May 2007
                              • 5390

                              #15
                              Originally posted by Kid Electric
                              Hmmm... it's been a while since I've coded, but I'm unfamiliar with this syntax.

                              Unfortunately, I tried it and it didn't seem to work, whereas the previous (longer) solution did. This one-line code seemed to return a response of "false" even if a single integer was entered. (using IE 7.0.6)
                              it uses regEx to check the value ... i cannot test it in IE 7 due to having no IE but it should work ... how do you apply the check?

                              kind regards

                              Comment

                              Working...