help !

Discussion in 'Javascript' started by Jean Jacques, Apr 30, 2005.

  1. Jean Jacques

    Jean Jacques Guest

    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 !
     
    Jean Jacques, Apr 30, 2005
    #1
    1. Advertising

  2. Jean Jacques

    Lee Guest

    Jean Jacques said:
    >
    >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 !


    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.
     
    Lee, Apr 30, 2005
    #2
    1. Advertising

  3. Jean Jacques

    RobG Guest

    Jean Jacques wrote:
    > 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 !!)


    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.value == 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.form))? 'All non-zero' :
    'At least one is zero');
    ">
    </form>


    --
    Rob
     
    RobG, Apr 30, 2005
    #3
  4. Jean Jacques

    Jean Jacques Guest

    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" <> a écrit dans le message de
    news:42736cef$0$31484$...
    > Jean Jacques wrote:
    > > 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 !!)

    >
    > 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.value == 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.form))? 'All non-zero' :
    > 'At least one is zero');
    > ">
    > </form>
    >
    >
    > --
    > Rob
     
    Jean Jacques, Apr 30, 2005
    #4
  5. RobG wrote:
    <snip>
    > 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.value == 0 will always be false.

    <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.
     
    Richard Cornford, Apr 30, 2005
    #5
  6. Jean Jacques

    RobG Guest

    Richard Cornford wrote:
    > RobG wrote:
    > <snip>
    >
    >>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.value == 0 will always be false.

    >
    > <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)
    >


    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.
     
    RobG, Apr 30, 2005
    #6
  7. RobG wrote:
    > Richard Cornford wrote:

    <snip>
    >> .. , so whenever either
    >> argument is of number type the other argument is
    >> exposed to the internal ToNumber function, ...

    <snip>
    > I tested it and you're right. No surprise there... :)


    Except that I should have used the term 'operand' in place of
    'argument', as that is what they are.

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


    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.
     
    Richard Cornford, Apr 30, 2005
    #7
  8. JRS: In article <42737370$0$23610$>, dated Sat, 30
    Apr 2005 14:00:32, seen in news:comp.lang.javascript, Jean Jacques
    <> posted :
    >
    >(for information , my second problem is english ) ...
    >


    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.demon.co.uk/news-use.htm> : about usage of News.
    No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
     
    Dr John Stockton, Apr 30, 2005
    #8
  9. Jean Jacques wrote:
    > 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 )

    <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.
     
    Stewart Gordon, May 10, 2005
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?dHJlbGxvdzQyMg==?=

    HELP! HELP! HELP! Opening Web Application Project Error

    =?Utf-8?B?dHJlbGxvdzQyMg==?=, Feb 20, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    805
    =?Utf-8?B?dHJlbGxvdzQyMg==?=
    Feb 20, 2004
  2. Harvey
    Replies:
    0
    Views:
    776
    Harvey
    Jul 16, 2004
  3. Harvey
    Replies:
    1
    Views:
    891
    Daniel
    Jul 16, 2004
  4. =?Utf-8?B?S2ltb24gSWZhbnRpZGlz?=

    HELP - HELP - HELP

    =?Utf-8?B?S2ltb24gSWZhbnRpZGlz?=, Mar 9, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    718
    Eliyahu Goldin
    Mar 9, 2006
  5. Buster

    Help, Help, Help

    Buster, Oct 4, 2003, in forum: Java
    Replies:
    3
    Views:
    495
    Saager
    Oct 30, 2003
Loading...

Share This Page