help !

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

    #1

    help !

    Hi ,

    How to check the values of a input fields in a java script when the there
    are something like this :


    field1
    field2
    field3
    field4
    field5
    field .. N

    the N value is in a hidden input , but when I try to do :

    function check ( form ) {
    var ok ;
    ok = false ;
    for (i=1; i <= n ; i++} {
    if (ok ) {
    ok = eval ( form.field+'i'+ .value !=0 )
    }
    }
    return ok ;
    }
    (this code isn't real , but my problem is ONLY the eval line !!)

    thanks for your help !




  • Lee

    #2
    Re: help !

    Jean Jacques said:[color=blue]
    >
    >Hi ,
    >
    >How to check the values of a input fields in a java script when the there
    >are something like this :
    >
    >
    >field1
    >field2
    >field3
    >field4
    >field5
    >field .. N
    >
    >the N value is in a hidden input , but when I try to do :
    >
    >function check ( form ) {
    >var ok ;
    >ok = false ;
    >for (i=1; i <= n ; i++} {
    > if (ok ) {
    > ok = eval ( form.field+'i'+ .value !=0 )
    > }
    >}
    > return ok ;
    >}
    >(this code isn't real , but my problem is ONLY the eval line !!)
    >
    >thanks for your help ![/color]

    1. Choose at least a half-way decent subject line.
    "help !" sucks, really, really badly.

    2. Never, ever use eval to evaluate the value of a form field.

    3. Never compare a value to zero, especially inside an eval.

    4. Never, ever, post code that isn't really your problem.

    Try again.

    Comment

    • RobG

      #3
      Re: help !

      Jean Jacques wrote:[color=blue]
      > Hi ,
      >
      > How to check the values of a input fields in a java script when the there
      > are something like this :
      >
      >
      > field1
      > field2
      > field3
      > field4
      > field5
      > field .. N
      >
      > the N value is in a hidden input , but when I try to do :
      >
      > function check ( form ) {
      > var ok ;
      > ok = false ;
      > for (i=1; i <= n ; i++} {
      > if (ok ) {
      > ok = eval ( form.field+'i'+ .value !=0 )
      > }
      > }
      > return ok ;
      > }
      > (this code isn't real , but my problem is ONLY the eval line !!)[/color]

      How can 'the eval line' be the only problem? Even if fixed, the
      script will never evaluate it.

      You set 'ok' to false, then try to do something only if it's true, so
      the 'something' will never occur.

      Your next issue is evaluating the value of a text field as a
      number. Text elements always return their value as a string, so
      even if the value in the input is '0', it will be evaluated as a
      string and form.fieldx.val ue == 0 will always be false.

      If what you are trying to do is return 'false' if any of the fields
      has a value of '0', then:

      <script type="text/javascript">
      function check( form ) {
      var el, i = '0';
      while ( el = form.elements['field' + ++i] ){
      if ( el.value && el.value == '0' ) return false;
      }
      return true;
      }
      </script>
      <form action="">
      <input type="text" name="field1" value="1"><br>
      <input type="text" name="field2" value="1"><br>
      <input type="text" name="field3" value="1"><br>
      <input type="text" name="field4" value="1"><br>
      <input type="button" value="name test" onclick="
      alert( (check(this.for m))? 'All non-zero' :
      'At least one is zero');
      ">
      </form>


      --
      Rob

      Comment

      • Jean Jacques

        #4
        Re: help !


        Thank's a lot for your reply and for your help,

        Sorry about my question ( your right about there is many errors ... gloups )
        , but I had no time this morning, and I had no the real code ( I had not
        internet connection in my work )

        But my problem was to find the correct syntax to evaluate a field.

        so, very very very thank's to take the time to give me the solution

        (for information , my second problem is english ) ...

        bye





        "RobG" <rgqld@iinet.ne t.auau> a écrit dans le message de
        news:42736cef$0 $31484$5a62ac22 @per-qv1-newsreader-01.iinet.net.au ...[color=blue]
        > Jean Jacques wrote:[color=green]
        > > Hi ,
        > >
        > > How to check the values of a input fields in a java script when the[/color][/color]
        there[color=blue][color=green]
        > > are something like this :
        > >
        > >
        > > field1
        > > field2
        > > field3
        > > field4
        > > field5
        > > field .. N
        > >
        > > the N value is in a hidden input , but when I try to do :
        > >
        > > function check ( form ) {
        > > var ok ;
        > > ok = false ;
        > > for (i=1; i <= n ; i++} {
        > > if (ok ) {
        > > ok = eval ( form.field+'i'+ .value !=0 )
        > > }
        > > }
        > > return ok ;
        > > }
        > > (this code isn't real , but my problem is ONLY the eval line !!)[/color]
        >
        > How can 'the eval line' be the only problem? Even if fixed, the
        > script will never evaluate it.
        >
        > You set 'ok' to false, then try to do something only if it's true, so
        > the 'something' will never occur.
        >
        > Your next issue is evaluating the value of a text field as a
        > number. Text elements always return their value as a string, so
        > even if the value in the input is '0', it will be evaluated as a
        > string and form.fieldx.val ue == 0 will always be false.
        >
        > If what you are trying to do is return 'false' if any of the fields
        > has a value of '0', then:
        >
        > <script type="text/javascript">
        > function check( form ) {
        > var el, i = '0';
        > while ( el = form.elements['field' + ++i] ){
        > if ( el.value && el.value == '0' ) return false;
        > }
        > return true;
        > }
        > </script>
        > <form action="">
        > <input type="text" name="field1" value="1"><br>
        > <input type="text" name="field2" value="1"><br>
        > <input type="text" name="field3" value="1"><br>
        > <input type="text" name="field4" value="1"><br>
        > <input type="button" value="name test" onclick="
        > alert( (check(this.for m))? 'All non-zero' :
        > 'At least one is zero');
        > ">
        > </form>
        >
        >
        > --
        > Rob[/color]


        Comment

        • Richard Cornford

          #5
          Re: help !

          RobG wrote:
          <snip>[color=blue]
          > Your next issue is evaluating the value of a text field as a
          > number. Text elements always return their value as a string, so
          > even if the value in the input is '0', it will be evaluated as a
          > string and form.fieldx.val ue == 0 will always be false.[/color]
          <snip>

          This is not true. The type-converting comparison operators have a strong
          preference for doing numeric comparison, so whenever either argument is
          of number type the other argument is exposed to the internal ToNumber
          function, type-converted to a number and the result evaluated with
          numeric comparison.

          Both the string "0" and the empty string are equal (using - == -) to
          numeric zero. So - x != 0 - is true for all non-empty strings that are
          not "0". That is generally not a very useful test to make, but there
          will be a few contexts where it is the appropriate test.

          (See ECMA 262 3rd edition, Section 11.9.3: Abstract Equality Comparison
          Algorithm)

          Richard.


          Comment

          • RobG

            #6
            Re: help !

            Richard Cornford wrote:[color=blue]
            > RobG wrote:
            > <snip>
            >[color=green]
            >>Your next issue is evaluating the value of a text field as a
            >>number. Text elements always return their value as a string, so
            >>even if the value in the input is '0', it will be evaluated as a
            >>string and form.fieldx.val ue == 0 will always be false.[/color]
            >
            > <snip>
            >
            > This is not true. The type-converting comparison operators have a strong
            > preference for doing numeric comparison, so whenever either argument is
            > of number type the other argument is exposed to the internal ToNumber
            > function, type-converted to a number and the result evaluated with
            > numeric comparison.
            >
            > Both the string "0" and the empty string are equal (using - == -) to
            > numeric zero. So - x != 0 - is true for all non-empty strings that are
            > not "0". That is generally not a very useful test to make, but there
            > will be a few contexts where it is the appropriate test.
            >
            > (See ECMA 262 3rd edition, Section 11.9.3: Abstract Equality Comparison
            > Algorithm)
            >[/color]

            I tested it and you're right. No surprise there... :-)

            Yes, the test seems a bit pointless, but I figured it may be the
            general concept that the OP was really after.

            --
            Rob.

            Comment

            • Richard Cornford

              #7
              Re: help !

              RobG wrote:[color=blue]
              > Richard Cornford wrote:[/color]
              <snip>[color=blue][color=green]
              >> .. , so whenever either
              >> argument is of number type the other argument is
              >> exposed to the internal ToNumber function, ...[/color][/color]
              <snip>[color=blue]
              > I tested it and you're right. No surprise there... :-)[/color]

              Except that I should have used the term 'operand' in place of
              'argument', as that is what they are.
              [color=blue]
              > Yes, the test seems a bit pointless, but I figured it may
              > be the general concept that the OP was really after.[/color]

              It is regrettably rare that a question is sufficiently specific for
              readers to be able to tell whether odd constructs (particularly tests)
              are actually intended or just the result of misconceptions. But
              comparisons with numeric zero is, as I recall, one of the expressions
              that Douglas Crockford's JSLINT flags. Probably because it would be
              difficult for any reader of such code to be certain of the exact
              intention (at least without explanation in comments).

              Richard.


              Comment

              • Dr John Stockton

                #8
                Re: help !

                JRS: In article <42737370$0$236 10$626a14ce@new s.free.fr>, dated Sat, 30
                Apr 2005 14:00:32, seen in news:comp.lang. javascript, Jean Jacques
                <jj.delavello@y ahoo.fr> posted :[color=blue]
                >
                >(for information , my second problem is english ) ...
                >[/color]

                Then give your question also in good but simple French. It'll be easier
                for educated people to read your French than your English.

                Note that the newsgroup FAQ does say "Please state your question as
                clearly and concisely as possible, ...", and its not reasonable to
                expect a Francophone to do that in English.

                --
                © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME ©
                Web <URL:http://www.uwasa.fi/~ts/http/tsfaq.html> -> Timo Salmi: Usenet Q&A.
                Web <URL:http://www.merlyn.demo n.co.uk/news-use.htm> : about usage of News.
                No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.

                Comment

                • Stewart Gordon

                  #9
                  Re: help !

                  Jean Jacques wrote:[color=blue]
                  > Thank's a lot for your reply and for your help,
                  >
                  > Sorry about my question ( your right about there is many errors ... gloups )
                  > , but I had no time this morning, and I had no the real code ( I had not
                  > internet connection in my work )[/color]
                  <snip>

                  Sometimes it's useful to prepare messages/SCCCEs* on the machine where
                  you do your programming, and then carry them on a floppy disk/USB
                  stick/whatever to wherever your Internet connection is in order to post
                  them.

                  * http://www.physci.org/codes/sscce.jsp

                  And another thing: It's JavaScript, not "java script".

                  Stewart.

                  --
                  My e-mail is valid but not my primary mailbox. Please keep replies on
                  the 'group where everyone may benefit.

                  Comment

                  Working...