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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dasty
    Recognized Expert New Member
    • Nov 2007
    • 101

    #16
    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)
    No, his regular expression check is right. It had to by some typo on your code that makes it to act incorrect. Try this:

    Code:
    <SCRIPT language=javascript>
    function checkNum(x)
    {
      if (!(/^\d+$/.test(x.value)))
      {
        alert("Only Numeric Values Allowed");
        x.focus();
        return false;
      }
      return true;
    }
    </SCRIPT>
    <input type="text" id="t_field" onChange='checkNum(this)'/>
    I just put above advice into the code. As far as i know IE does not have any problems with regular expressions at all. But if you want to check something, you have to write clear what exact format of numbers do you want to validate. Do you want to allow negative / positive numbers? Do you want do check decimal numbers?
    Last edited by acoder; Dec 20 '11, 11:32 AM.

    Comment

    • Akshay Accent
      New Member
      • Dec 2008
      • 1

      #17
      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
      For allowing only numeric values to be placed inside a text box using JavaScript, we need to capture the “onKeyDown ” event for the keyboard. This check is done on client side whenever a user enters a value in the textbox using keyboard keys.

      Example of such a script is:

      TextBoxTest.Att ributes.Add(”on KeyDown”, “if((event.keyC ode >= 48 && event.keyCode <= 57)||(event.key Code >= 96 && event.keyCode <= 105)||(event.ke yCode == 8 ) ||(event.keyCod e == 9) || (event.keyCode == 12) || (event.keyCode == 27) || (event.keyCode == 37) || (event.keyCode == 39) || (event.keyCode == 46) ){return true;}else{retu rn false;}”);

      Regards,
      Akshay Jain
      Last edited by acoder; Dec 20 '11, 11:31 AM.

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #18
        Akshay, welcome to Bytes and thanks for your contribution.

        Unfortunately, the code you've posted is IE-specific and would not work cross-browser. In addition to that, this being the JavaScript forum, it's better to avoid .NET code and provide just the JavaScript.

        Thanks.

        Comment

        • deep4ut
          New Member
          • Dec 2011
          • 4

          #19
          yes you can use isNaN() function of javascript...

          Comment

          • rajeshsundu
            New Member
            • Oct 2013
            • 3

            #20
            Code:
            function chk(val)
            {
            var a=val.parseInt();
            if(a == a+1-1)//answer becomes true only if they are numbers
            {
            alert("its a number");
            }
            else
            {
            alert("not a number");
            }
            Last edited by Rabbit; Oct 15 '13, 06:49 PM. Reason: Please use [CODE] and [/CODE] tags when posting code or formatted data.

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #21
              Have you tested your code to see if it works? It seems a very convoluted way to check for integers. See https://developer.mozilla.org/en-US/...jects/parseInt

              Comment

              • rajeshsundu
                New Member
                • Oct 2013
                • 3

                #22
                Code:
                function chk(val)
                    {
                    var a=parseInt(val);
                    if(a == a+1-1)//answer becomes true only if they are numbers
                    {
                    alert("its a number");
                    }
                    else
                    {
                    alert("not a number");
                    }
                This works bro :)
                Last edited by Dormilich; Dec 1 '13, 12:07 PM.

                Comment

                • Dormilich
                  Recognized Expert Expert
                  • Aug 2008
                  • 8694

                  #23
                  there is no need for the a+1-1 bit. if a were NaN (the only value parseInt() gives on failure), even then a==a would be false.

                  Comment

                  • rajeshsundu
                    New Member
                    • Oct 2013
                    • 3

                    #24
                    yes you are right @dormilich:)i'm new to programming and just now learning things..

                    Comment

                    Working...