help !

J

Jean Jacques

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 !
 
L

Lee

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.
 
R

RobG

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

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

Jean Jacques

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

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

Richard Cornford

RobG wrote:
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.
 
R

RobG

Richard said:
RobG wrote:


<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.
 
R

Richard Cornford

RobG said:
Richard Cornford wrote:
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.
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Sat, 30
Apr 2005 14:00:32, seen in Jean Jacques
(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.
 
S

Stewart Gordon

Jean said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top