Help with some JS code

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mbuna@yahoo.com

    Help with some JS code

    I have the following code in a page, but it always returns true.

    if (pagevalue >=1 && pagevalue <= maxpages);
    {
    return true;
    }

    return false;



    I can enter 0 for pagevalue or 10*maxpages for page value and it always
    returns true.

    What am I doing wrong?

    TIA

  • Evertjan.

    #2
    Re: Help with some JS code

    wrote on 23 feb 2005 in comp.lang.javas cript:
    [color=blue]
    > I have the following code in a page, but it always returns true.
    >
    > if (pagevalue >=1 && pagevalue <= maxpages);
    > {
    > return true;
    >}
    > return false;[/color]

    this is the same as:

    return pagevalue>=1 && pagevalue<=maxp ages;

    [color=blue]
    > I can enter 0 for pagevalue or 10*maxpages for page value and it always
    > returns true.[/color]

    test:

    pagevalue=2;
    maxpages=1;
    r= (pagevalue>=1) && (pagevalue<=max pages);
    alert(r);

    shows: false

    pagevalue=0;
    maxpages=100;
    r= pagevalue>=1 && pagevalue<=maxp ages;
    alert(r);

    shows: false


    --
    Evertjan.
    The Netherlands.
    (Replace all crosses with dots in my emailaddress)

    Comment

    • Erwin Moller

      #3
      Re: Help with some JS code

      mbuna@yahoo.com wrote:
      [color=blue]
      > I have the following code in a page, but it always returns true.
      >
      > if (pagevalue >=1 && pagevalue <= maxpages);
      > {
      > return true;
      > }
      >
      > return false;
      >
      >
      >
      > I can enter 0 for pagevalue or 10*maxpages for page value and it always
      > returns true.
      >
      > What am I doing wrong?
      >
      > TIA[/color]

      Hi,

      I have 3 things to say:
      First, use () to order your logic.

      My guess is that you ment:

      if ( (pagevalue >=1) && (pagevalue <= maxpages) )
      {
      return true;
      } else {
      return false;
      }

      second:
      You wrote:
      if (pagevalue >=1 && pagevalue <= maxpages);

      Did you see the ; at the end?

      Third:
      Even if you used:
      if ( (pagevalue >=1) && (pagevalue <= maxpages) )
      the function should return false for 0 and 10* maxpages.
      It only returns true when it is INBETWEEN (and including) 1 and maxpages.

      Regards,
      Erwin Moller



      Comment

      • mbuna@yahoo.com

        #4
        Re: Help with some JS code

        Neither of those worked. Both caught 0 as invalid but neither caught
        the high end. Here's the full function, it was writen by someone else
        and I'm just trying to make it work.



        function validate(objFrm )
        {
        var maxpages="<?=$n um_pages?>";
        var pno=numchk(objF rm.pageno.value );
        var pagevalue=(objF rm.pageno.value );

        if(pno=="no")
        {
        alert("Enter a number between 1 and "+ maxpages +". You entered "
        + pagevalue +".");
        objFrm.pageno.s elect();
        return false;
        }

        if ( (1 <= pagevalue) && (pagevalue <= maxpages) )
        {
        return true;
        }

        alert("Enter a number between 1 and "+ maxpages +". You entered " +
        pagevalue +".");
        objFrm.pageno.s elect();
        return false;
        }

        Comment

        • Erwin Moller

          #5
          Re: Help with some JS code

          mbuna@yahoo.com wrote:
          [color=blue]
          > Neither of those worked. Both caught 0 as invalid but neither caught
          > the high end. Here's the full function, it was writen by someone else
          > and I'm just trying to make it work.
          >[/color]

          Hi,

          I made you a simple script, that tests what you try to do.
          It works as expected.

          -----------------------------

          <html>
          <head>
          <script type="text/javascript">
          function test() {
          var pagevalue = document.forms. testform.myNum. value;
          var maxpages = document.forms. testform.maxpag es.value;

          if ( (1 <= pagevalue) && (pagevalue <= maxpages) )
          {
          alert ("true!");
          } else {
          alert ("false!");
          }

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

          <form name="testform" >
          pagevalue: <input type="text" name="myNum">
          <br>
          maxpages: <input type="text" name="maxpages" >
          <br>
          <input type="button" onClick="test() ;" value="doTest">
          </form>

          </body>
          </html>

          ---------------------------

          So I think the problem must be somewhere else.
          Try adding alerts under:
          var pno=numchk(objF rm.pageno.value );
          var pagevalue=(objF rm.pageno.value );

          to see if pno and pagevalue contain values you expect.
          Actually, I alway debug by adding alerts, and in 90% of the cases I very
          quickly find that I don't get the values in my vars that I expected in the
          first place.

          Also, casting values to Integer (ParseInt) can help.

          Hope this helps. :-)

          Regards,
          Erwin Moller

          Comment

          • denisb

            #6
            Re: Help with some JS code

            <mbuna@yahoo.co m> wrote:[color=blue]
            > I have the following code in a page, but it always returns true.
            > What am I doing wrong?[/color]
            [color=blue]
            > if (pagevalue >=1 && pagevalue <= maxpages);[/color]
            just this _______________ _______________ _____^


            (the ;)

            --
            @@@@@
            E -00 comme on est very beaux dis !
            ' `) /
            |\_ =="

            Comment

            Working...