isNum

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rick

    #1

    isNum

    I have been trying to get this code to work and the isNum seem to be giving
    me a problem and I just cannot figure it out any help would me very much
    appreciated!!

    Thanks in advance!!!

    function SubmitSame() {
    var usernew, QPSQTY, PTPS, PSSQTY, PSST, ECQTY, ECT, EXQTY, EXT, INQTY,
    INTA, SKQTY, SKT, CSQTY, CST, total, result, result1, result2, result3,
    result4, result5, result6, result0;
    if (document.getEl ementById) {
    usernew = document.getEle mentById('usern ew');
    QPSQTY = document.getEle mentById('QPSQT Y');
    PTPS = document.getEle mentById('PTPS' );
    PSSQTY = document.getEle mentById('PSSQT Y');
    PSST = document.getEle mentById('PSST' );
    ECQTY = document.getEle mentById('ECQTY ');
    ECT = document.getEle mentById('ECT') ;
    EXQTY = document.getEle mentById('EXQTY ');
    EXT = document.getEle mentById('EXT') ;
    INQTY = document.getEle mentById('INQTY ');
    INTA = document.getEle mentById('INTA' );
    SKQTY = document.getEle mentById('SKQTY ');
    SKT = document.getEle mentById('SKT') ;
    CSQTY = document.getEle mentById('CSQTY ')
    CST = document.getEle mentById('CST') ;
    total = document.getEle mentById('total ');
    }
    else {
    usernew = document.userne w;
    QPSQTY = usernew.QPSQTY;
    PTPS = usernew.PTPS;
    PSSQTY = usernew.PSSQTY;
    PSST = usernew.PSST;
    ECQTY = usernew.ECQTY;
    ECT = usernew.ECT;
    EXQTY = usernew.EXQTY;
    EXT = usernew.EXT;
    INQTY = usernew.INQTY;
    INTA = usernew.INTA;
    SKQTY = usernew.SKQTY;
    SKT = usernew.SKT;
    CSQTY = usernew.CSQTY;
    CST = usernew.CST;
    total = usernew.total;
    }
    if (!isNum(QPSQTY. value)) {
    return ; }
    }



  • Fred Oz

    #2
    Re: isNum

    Rick wrote:[color=blue]
    > I have been trying to get this code to work and the isNum seem to be giving
    > me a problem and I just cannot figure it out any help would me very much
    > appreciated!!
    >[/color]
    [...][color=blue]
    > }
    > if (!isNum(QPSQTY. value)) {
    > return ; }
    > }[/color]

    I think you are confused with isNaN(), which returns true if the
    argument is not a number.

    isNaN(QPSQTY.va lue)

    will return true if the text entered into QPSQTY is not a
    number. Here, play with this:

    <input type="text" value="6" onblur="
    document.getEle mentById('xx'). innerHTML =
    this.value + ' isNaN? : ' + isNaN(this.valu e);
    ">
    <span id="xx"></span>


    e.g. 23e345 *is* a valid number and will return false.
    .23 is a valid number and will return false.
    123qwe is not a valid number and will return true.



    --
    Fred

    Comment

    • Rick

      #3
      Re: isNum

      Wow that worked perfect.

      What is the isNum for?

      Thanks you very much!!!


      "Fred Oz" <OzFred@iinet.n et.auau> wrote in message
      news:KDs%d.440$ Zn.26430@news.o ptus.net.au...[color=blue]
      > Rick wrote:[color=green]
      >> I have been trying to get this code to work and the isNum seem to be
      >> giving
      >> me a problem and I just cannot figure it out any help would me very much
      >> appreciated!!
      >>[/color]
      > [...][color=green]
      >> }
      >> if (!isNum(QPSQTY. value)) {
      >> return ; }
      >> }[/color]
      >
      > I think you are confused with isNaN(), which returns true if the
      > argument is not a number.
      >
      > isNaN(QPSQTY.va lue)
      >
      > will return true if the text entered into QPSQTY is not a
      > number. Here, play with this:
      >
      > <input type="text" value="6" onblur="
      > document.getEle mentById('xx'). innerHTML =
      > this.value + ' isNaN? : ' + isNaN(this.valu e);
      > ">
      > <span id="xx"></span>
      >
      >
      > e.g. 23e345 *is* a valid number and will return false.
      > .23 is a valid number and will return false.
      > 123qwe is not a valid number and will return true.
      >
      >
      >
      > --
      > Fred[/color]


      Comment

      • Fred Oz

        #4
        Re: isNum

        Rick wrote:[color=blue]
        > Wow that worked perfect.
        >
        > What is the isNum for?[/color]

        Nothing.
        [color=blue]
        >
        > Thanks you very much!!!
        >[/color]

        You're welcome.

        [...]


        As a tip, here is a better way of doing what you are doing. It
        does two things:

        Puts your variables into objects, one for text one for numbers,
        and uses the forms collection to avoid using getElementById.

        You could include the DynWrite[1] function to provide
        getElementById for those browsers that support document.all, but
        I think the forms collection is simpler (assuming that you are
        using a form...)

        The function I've written just changes the style of a form
        element depending on whether isNaN returns true or false, hardly
        an exhaustive test of whether the input really is a number but
        illustrative anyway.

        I could have done the isNaN test when reading the values, but
        that wouldn't show getting the values back from the object.

        1. <URL:http://www.jibbering.c om/faq/faq_notes/alt_dynwrite.ht ml>


        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
        "http://www.w3.org/TR/html4/strict.dtd">
        <html><head>
        <title>Mod Style Play</title>
        <meta http-equiv="Content-Type"
        content="text/html; charset=iso-8859-1">
        <style type="text/css">
        ..isText {font-weight: normal; color: blue;}
        ..isNumber {font-weight: bold; color: red;}
        </style>
        <script type="text/JavaScript">

        function SubmitSame(theF orm) {
        // Just a guess, but are these the names
        // of inputs in a form? If so, put them in an array

        // all the text elements (just guessing)
        var txtIn = {
        'usernew':'','Q PSQTY':'','PTPS ':'','PSSQTY':' ','PSST':'',
        'ECQTY':'','ECT ':'','EXQTY':''
        };
        // all the number elements (just guessing)
        var numIn = {
        'EXT':'','INQTY ':'','INTA':'', 'SKQTY':'','SKT ':'',
        'CSQTY':'','CST ':'','total':''
        };
        var result, result1, result2, result3,
        result4, result5, result6, result0, t, n;

        // Now just loop through the elements of the objects and use
        // the forms collection - no getElementById/all issues
        for ( t in txtIn) {
        txtIn[t] = theForm.element s[t].value;
        }
        for ( n in txtIn) {
        txtIn[n] = theForm.element s[n].value;
        }

        // Test to see if isNaN & modify class accordingly
        // Demonstrates looping through elements of an object
        for (t in txtIn) {
        theForm.element s[t].className =
        (isNaN(txtIn[t]))? 'isText':'isNum ber';
        }
        for (n in numIn) {
        theForm.element s[n].className =
        (isNaN(numIn[n]))? 'isText':'isNum ber';
        }

        }
        </script>
        </head>
        <body>

        <form name="formA" action="">
        <div>
        <input type="text" size="20" name="usernew"> usernew</input><br>
        <input type="text" size="20" name="QPSQTY">Q PSQTY</input><br>
        <input type="text" size="20" name="PTPS">PTP S</input><br>
        <input type="text" size="20" name="PSSQTY">P SSQTY</input><br>
        <input type="text" size="20" name="PSST">PSS T</input><br>
        <input type="text" size="20" name="ECQTY">EC QTY</input><br>
        <input type="text" size="20" name="ECT">ECT</input><br>
        <input type="text" size="20" name="EXQTY">EX QTY</input><br>
        <input type="text" size="20" name="EXT">EXT</input><br>
        <input type="text" size="20" name="INQTY">IN QTY</input><br>
        <input type="text" size="20" name="INTA">INT A</input><br>
        <input type="text" size="20" name="SKQTY">SK QTY</input><br>
        <input type="text" size="20" name="SKT">SKT</input><br>
        <input type="text" size="20" name="CSQTY">CS QTY</input><br>
        <input type="text" size="20" name="CST">CST</input><br>
        <input type="text" size="20" name="total">to tal</input><br>
        <input type="reset">
        <input type="button" value="stuff"
        onclick="Submit Same(this.form) ;">
        </div>
        </form>
        <span id="debugText"> </span>
        <br><br>

        <input type="text" value="6" onblur="
        document.getEle mentById('xx'). innerHTML =
        this.value + ' isNaN? : ' + isNaN(this.valu e);
        ">
        <span id="xx"></span></body>
        </html>



        --
        Fred



        Comment

        • Fred Oz

          #5
          Re: isNum

          Rick wrote:[color=blue]
          > Wow that worked perfect.
          >
          > What is the isNum for?[/color]

          Nothing.
          [color=blue]
          >
          > Thanks you very much!!![/color]

          You're welcome.

          [...]


          As a tip, here is a better way of doing what you are doing. It
          does two things:

          Puts your variables into objects, one for text one for numbers,
          and uses the forms collection to avoid using getElementById.

          You could include the DynWrite[1] function to provide
          getElementById for those browsers that support document.all, but
          I think the forms collection is simpler (assuming that you are
          using a form...)

          The function I've written just changes the style of a form
          element depending on whether isNaN returns true or false, hardly
          an exhaustive test of whether the input really is a number but
          illustrative anyway.

          I could have done the isNaN test when reading the values, but
          that wouldn't show getting the values back from the object.

          1. <URL:http://www.jibbering.c om/faq/faq_notes/alt_dynwrite.ht ml>


          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
          "http://www.w3.org/TR/html4/strict.dtd">
          <html><head>
          <title>Mod Style Play</title>
          <meta http-equiv="Content-Type"
          content="text/html; charset=iso-8859-1">
          <style type="text/css">
          ..isText {font-weight: normal; color: blue;}
          ..isNumber {font-weight: bold; color: red;}
          </style>
          <script type="text/JavaScript">

          function SubmitSame(theF orm) {
          // Just a guess, but are these the names
          // of inputs in a form? If so, put them in an array

          // all the text elements (just guessing)
          var txtIn = {
          'usernew':'','Q PSQTY':'','PTPS ':'','PSSQTY':' ','PSST':'',
          'ECQTY':'','ECT ':'','EXQTY':''
          };
          // all the number elements (just guessing)
          var numIn = {
          'EXT':'','INQTY ':'','INTA':'', 'SKQTY':'','SKT ':'',
          'CSQTY':'','CST ':'','total':''
          };
          var result, result1, result2, result3,
          result4, result5, result6, result0, t, n;

          // Now just loop through the elements of the objects and use
          // the forms collection - no getElementById/all issues
          for ( t in txtIn) {
          txtIn[t] = theForm.element s[t].value;
          }
          for ( n in numIn) {
          numIn[n] = theForm.element s[n].value;
          }

          // Test to see if isNaN & modify class accordingly
          // Demonstrates looping through elements of an object
          for (t in txtIn) {
          theForm.element s[t].className =
          (isNaN(txtIn[t]))? 'isText':'isNum ber';
          }
          for (n in numIn) {
          theForm.element s[n].className =
          (isNaN(numIn[n]))? 'isText':'isNum ber';
          }

          }
          </script>
          </head>
          <body>

          <form name="formA" action="">
          <div>
          <input type="text" size="20" name="usernew"> usernew</input><br>
          <input type="text" size="20" name="QPSQTY">Q PSQTY</input><br>
          <input type="text" size="20" name="PTPS">PTP S</input><br>
          <input type="text" size="20" name="PSSQTY">P SSQTY</input><br>
          <input type="text" size="20" name="PSST">PSS T</input><br>
          <input type="text" size="20" name="ECQTY">EC QTY</input><br>
          <input type="text" size="20" name="ECT">ECT</input><br>
          <input type="text" size="20" name="EXQTY">EX QTY</input><br>
          <input type="text" size="20" name="EXT">EXT</input><br>
          <input type="text" size="20" name="INQTY">IN QTY</input><br>
          <input type="text" size="20" name="INTA">INT A</input><br>
          <input type="text" size="20" name="SKQTY">SK QTY</input><br>
          <input type="text" size="20" name="SKT">SKT</input><br>
          <input type="text" size="20" name="CSQTY">CS QTY</input><br>
          <input type="text" size="20" name="CST">CST</input><br>
          <input type="text" size="20" name="total">to tal</input><br>
          <input type="reset">
          <input type="button" value="stuff"
          onclick="Submit Same(this.form) ;">
          </div>
          </form>
          <span id="debugText"> </span>
          <br><br>

          <input type="text" value="6" onblur="
          document.getEle mentById('xx'). innerHTML =
          this.value + ' isNaN? : ' + isNaN(this.valu e);
          ">
          <span id="xx"></span></body>
          </html>

          --
          Fred

          Comment

          • Alvaro G Vicario

            #6
            Re: isNum

            *** Rick wrote/escribió (Mon, 21 Mar 2005 04:09:03 GMT):[color=blue]
            > I have been trying to get this code to work and the isNum seem to be giving
            > me a problem and I just cannot figure it out any help would me very much
            > appreciated!![/color]

            I can think of this:

            function isNum(data){
            return typeof(data)==' number';
            }

            document.writel n(isNum());
            document.writel n(isNum(-2));
            document.writel n(isNum(0));
            document.writel n(isNum(33));
            document.writel n(isNum(3.1416) );
            document.writel n(isNum('951')) ;
            document.writel n(isNum('foo')) ;
            document.writel n(isNum(documen t));
            document.writel n(isNum(this));

            Returns:

            false
            true
            true
            true
            true
            false
            false
            false
            false


            --
            -- Álvaro G. Vicario - Burgos, Spain
            -- Don't e-mail me your questions, post them to the group
            --

            Comment

            • Dr John Stockton

              #7
              Re: isNum

              JRS: In article <Vuu%d.450$Zn.2 6667@news.optus .net.au>, dated Mon, 21
              Mar 2005 07:13:57, seen in news:comp.lang. javascript, Fred Oz
              <OzFred@iinet.n et.auau> posted :[color=blue]
              >
              > You could include the DynWrite[1] function to provide
              > getElementById for those browsers that support document.all,[/color]
              [color=blue]
              > ...[/color]
              [color=blue]
              >1. <URL:http://www.jibbering.c om/faq/faq_notes/alt_dynwrite.ht ml>[/color]


              There is a confusion somewhere.

              DynWrite is in the FAQ proper, section 4.15 : it does not provide
              getElementById, but it provides a means of doing one of the things that
              is commonly done with getElementById.

              There may be a different implementation of DynWrite in the Notes; but
              any function of that name there should have the same parameters and
              result.


              I now think that it would be well to use inline code to provide
              getElementById, and then to have a simple DynWrite using getElementById
              (if wanted in more than one or two places; for clarity and compactness).



              It's not generally sensible to use isNaN or its logical inverse to
              validate numeric input, since in most cases one does not want to accept
              all possible representations of all possible IEEE Doubles. Far better
              to use RegExps : <URL:http://www.merlyn.demo n.co.uk/js-valid.htm>.

              --
              © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
              <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
              <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
              <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

              Comment

              Working...