Assign value to control

C

ckerns

I want to loop thru an array of controls,(39 of them...defaults = 0).
If value is null or non-numeric I want to assign the value of "0".

rowString = "L411" //conrol name

if (isNaN(eval ("document.forms[0]."+rowString+".value")) == true )
{
//this alert works if the value is a letter,i.e,"a"
alert("You have entered an non-numeric value.\nEnter a number
in the appropriate box.");

//If I leave this in the code dies
//document.forms[0]." + rowString + ".value = 0;
}



I've played with this, but no luck.




if (isNaN(eval ("document.forms[0]."+rowString+".value")) == true ||
isEmpty(eval ("document.forms[0]."+rowString+".value")) == true )
{
//this alert works if the value is a letter,i.e,"a"
alert("You have entered an non-numeric value.\nEnter a number
in the appropriate box.");

//If I leave this in the code dies
//document.forms[0]." + rowString + ".value = 0;
}

Any hints here?


Thansk

ck
 
E

Evertjan.

wrote on 15 apr 2005 in comp.lang.javascript:
if (isNaN(eval ("document.forms[0]."+rowString+".value")) == true )

If you follow this NG for a while, you will notice some rules that can help
you:

1 eval() is evil, you do not need it

2 testing in an if clause if something is true
is the same as just using the something.

if (isNaN(document.forms[0].elements[rowString].value))
alert("not a number")
 
R

RobB

I want to loop thru an array of controls,(39 of them...defaults = 0).
If value is null or non-numeric I want to assign the value of "0".

rowString = "L411" //conrol name

if (isNaN(eval ("document.forms[0]."+rowString+".value")) == true )
{
//this alert works if the value is a letter,i.e,"a"
alert("You have entered an non-numeric value.\nEnter a number
in the appropriate box.");

//If I leave this in the code dies

------> (probably because I forgot the eval()!)
//document.forms[0]." + rowString + ".value = 0;
}



I've played with this, but no luck.




if (isNaN(eval ("document.forms[0]."+rowString+".value")) == true ||
isEmpty(eval ("document.forms[0]."+rowString+".value")) == true )
{
//this alert works if the value is a letter,i.e,"a"
alert("You have entered an non-numeric value.\nEnter a number
in the appropriate box.");

//If I leave this in the code dies
//document.forms[0]." + rowString + ".value = 0;
}

Any hints here?


Thansk

ck

What does 'an array of controls' mean?

You can put the names in a (real) array:

var cnames = ['L411' , 'M58' , 'j54' ....]

....and loop *that*...

var el, els = document.forms[0].elements;
for (var i = 0, l = cnames.length; i < l; ++i)
{
if ((el = els[cnames])
&& /^\D*$/.test(el.value))
el.value = '0';
}
 
C

ckerns

I am newly.

Why not eval...any links?

I tried code, but no luck...

thx

wrote on 15 apr 2005 in comp.lang.javascript:
if (isNaN(eval ("document.forms[0]."+rowString+".value")) == true )

If you follow this NG for a while, you will notice some rules that can help
you:

1 eval() is evil, you do not need it

2 testing in an if clause if something is true
is the same as just using the something.

if (isNaN(document.forms[0].elements[rowString].value))
alert("not a number")
 
E

Evertjan.

wrote on 16 apr 2005 in comp.lang.javascript:
wrote on 15 apr 2005 in comp.lang.javascript:
if (isNaN(eval ("document.forms[0]."+rowString+".value")) == true )

If you follow this NG for a while, you will notice some rules that can
help you:

1 eval() is evil, you do not need it

2 testing in an if clause if something is true
is the same as just using the something.

if (isNaN(document.forms[0].elements[rowString].value))
alert("not a number")

[please do not toppost. Topposting corrected]
I am newly.

Why not eval...any links?

Sure, read the archive of this NG

<http://groups-beta.google.com/groups?q=eval+is+evil&safe=off>

3410 hits
I tried code, but no luck...

Programming is not a question of luck,
but of basic knowledge, testing and debugging.

Try this file:

========= test.html =========


<form>
row 0: <input value="3"><br>
row 1: <input value="blah">
</form>

<script type='text/javascript'>

var rowString=0

if(isNaN(document.forms[0].elements[rowString].value))
alert("row " + rowString + " value is not a number")
else
alert("row " + rowString + " value is is a number")

rowString=1

if(isNaN(document.forms[0].elements[rowString].value))
alert("row " + rowString + " value is not a number")
else
alert("row " + rowString + " value is is a number")

</script>

===============================

Communication would be easier if you give a [nick]name, btw.
 
C

ckerns

Oops...

if ((el = els[cnames])

I tried hard, but I got lost here. I googled special charcter pages,
but I'm still unsure what this means.

I am sorry because I know you put effort into this.

I'm only 14, so don't get mad at me...I'm trying real hard.


ck
&& !/^\d+$/.test(el.value))

[Friday]
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Fri, 15
Apr 2005 20:40:23, seen in Evertjan.
if (isNaN(document.forms[0].elements[rowString].value))
alert("not a number")

Since for most applications the set of allowable and reasonable inputs
is much smaller than the set of all inputs that isNaN will accept (the
latter is in fact infinite; or, rather, bounded by outside
considerations), ISTM that the above test is weaker than might be
appropriate.

if (!/\d+/.test(document.forms[0].elements[rowString].value))
alert('not good') // or /\d+\.\d\d/

might be a better starting-point.
 
E

Evertjan.

Dr John Stockton wrote on 16 apr 2005 in comp.lang.javascript:
JRS: In article <[email protected]>, dated Fri, 15
Apr 2005 20:40:23, seen in Evertjan.
if (isNaN(document.forms[0].elements[rowString].value))
alert("not a number")

Since for most applications the set of allowable and reasonable inputs
is much smaller than the set of all inputs that isNaN will accept (the
latter is in fact infinite; or, rather, bounded by outside
considerations), ISTM that the above test is weaker than might be
appropriate.

if (!/\d+/.test(document.forms[0].elements[rowString].value))
alert('not good') // or /\d+\.\d\d/

might be a better starting-point.

You are right of course, John, I just wanted to show the right way of:

document.forms[0].elements[rowString].value

in answer on a "Does not work" statement.
 
R

Richard Cornford

RobB wrote:
if ((el = els[cnames])

I tried hard,


What did you try?
but I got lost here.

What do you mean by 'got lost'. Generally, the more effort you put in to
asking questions the better the answers you get will be. And RodB asked
you a question, where is its answer?
I googled special charcter pages,
Why?

but I'm still unsure what this means.

You are responding to a quote of half of the opening part of an - if -
statement. The full statement (without contents) was:-

You are responding to a quote of half of the opening part of an - if -
statement. The full statement was:-

if ((el = els[cnames]) && /^\D*$/.test(el.value))
el.value = '0';

General programming advice (and the well known JSLINT) would insist that
the statement following the condition expression in an - if - statement
should always, and only, be a block statement (which would then contain
the statement that would otherwise follow the - if - expression). The
language doesn't require this but it avoids errors when you find you
need to ad another statement. Even experienced programmers trip up when
doing that so insisting on the block statement is a worthwhile habit to
acquire. So the formatting would be:-

if ((el = els[cnames]) && /^\D*$/.test(el.value)){
el.value = '0';
}

The ECMA 262 specified production rule for an if statement is:-

if ( Expression ) Statement

In the case of this - if - statement the expression is a logical AND
expression. Logical AND is a binary expression, in that its operator -
&& - has an operand to its left and its right (two operands, hence
binary)

The operand on the left-hand side is evaluated and the result of that
evaluation is type-converted to a boolean (true or false) value. If it
is false then the result of the logical AND expression is the evaluated
result of the left-hand side expression (not the value following its
type-conversion to boolean). If the result is false then the right hand
side is evaluated and that becomes the result of the logical AND
expression.

The - if - statement decides whether to execute the statement it
contains based on the value of the Expression, that expression is the
logical AND expression. The result of that expression is type-converted
to boolean and if the result of that is true then the contained
statement is executed, otherwise it is skipped.

The left hand side of the logical AND expression is the expression:-

(el = els[cnames])

The right hand side of the logical AND expression is the expression:-

/^\D$/.test(el.value)

This right-hand side expression is a method call on an object. The
object is a regular expression literal. It is a regular expression
object that will math zero or more occurrences of non-decimal digit
characters making up the entire contents of a string:-

^ <-- the start of a string
/D <-- non-decimal digit character
* <-- zero or more occurrences
$ <-- the end of a string

Regular expressions are objects and objects may have methods; functions
that may be executed in association with the specific instance of the
object in question. The Regular expression object has a method called -
test - and the dot-notation property accessor - /^\D*/.test - refers to
that method of the object (in a way that retains the association of the
function with the object.

Following a property accessor that refers to a function with an
expression that represents an arguments list (which may be empty) is a
call to that function. The function is executed, and as the association
with the object exist the function is executed as a method of that
object.

The test function takes a string argument (or type-converts its argument
into a string if it is not of string primitive type to start with). If
the regular expression matches the string argument then the method call
returns the boolean value true, otherwise it returns false. Thus the
right hand side of the logical AND expression will evaluate as boolean
true or false, depending on the argument.

The argument is another dot notation property accessor - el.value -. The
identifier - el - is resolved against the scope chain and a value is the
result. That value is then type-converted into an object, which does not
involve doing anything here, as it would be an object to start with. (By
'an object' I mean a reference to an object. That is; the value of -
el - is a reference to an object). The identifier after the dot is then
resolved as a named property of that object. the result of the property
accessor is the value of that named property of the object.

As - el - is expected to refer to an INPUT element in the DOM and INPUT
elements have 'value' properties that refer to the string value of the
INPUT element, the dot notation property accessor is resolved as that
string value. That string value becomes the argument to the - test -
method call on the regular expression object, which will return boolean
true or false.

The left had side of the logical AND expression:-

(el = els[cnames])

- is an assignment expression. The right-hand side of the assignment
operator - = - is evaluated and its value is assigned to the
identifier - el - (or, in reality, to the property of an object on the
scope chain with the name '"el") on the left-hand side of the
assignment.

You will probably have noticed that expressions tend to be made up of
simpler expressions, and that expressions result in values. The
assignment expression, while doing something specific, also results in a
value. The value of the entire assignment expression is the value
resulting from the evaluation of the right-hand side, as assigned to the
left-hand side.

The right hand side of the assignment expression is a bracket notation
property accessor. Bracket notation property accessors are just like
dot-notation property accessors in terms of what they do, but they are
more flexible because instead of stating an identifier as the property
name after the dot they may use any string value (or any expression that
will result in a string value, or can be type-converted into a string).

(Bracket notation also allows property names to consist of any character
sequence rather than the restricted set that qualify as an Identifier.
This is particularly important in the case of array indexes as
Identifiers may not start with a decimal digit, and array indexes may
only contain decimal digits)

Thus:-

document.body

- is the equivalent of:-

document["body"]

In the property accessor - els[cnames] - the expression that will
result in the name that is to be resolved as a named property of the -
els - object, is the expression - cnames -. That expression is
another bracket notation property accessor, with the Identifier - i -
being resolved to provide the name for a property of the object referred
to by the identifier - cnames -.

cnames - is an array of strings and - i - is an integer value so -
cnames - results in one of the strings in the - cnames - array
(assuming the integer is within the appropriate range). That string is
then used as the name of a property of the - els - object, and the
result of the expression is whatever value is held by whichever name is
the result of the indexed reference to the array of strings.

els - has been assigned a reference to the - elements - collection of
the first FORM element in the document. The - cnames - array is expected
to hold a list of all of the names or form controls within that form. So
the right hand side of the assignment expression is expected to result
in a reference to one of the form control objects. It is that reference
that is assigned to the - el - identifier. The result of the entire
assignment expression is also the reference to that object, which is
then type-converted into a boolean value to determine the behaviour of
the logical AND expression.

The advantage of this is that if the right hand side of the assignment
expression doe not resolve as a reference to a form control then the
result of the assignment expression will be the value - undefined -
(which, potentially confusingly, is a distinct and specific primitive
value in javascript). When the logical AND expression evaluates its
left-hand side, and the assignment expression results in - undefined -
then the type-converted boolean value is false, so the logical AND
expression returns false (and its right-hand side is not evaluated).
This short circuiting in the logical AND expression mans that its right
hand side will only be executed if - el - is assigned a value that
type-converts to boolean true, which all object references do, and
avoids that right hand side erroring (as it must if it tried to evaluate
the property accessor - el.value - when - el - had been assigned -
undefined -).
I am sorry because I know you put effort into this.

I'm only 14, so don't get mad at me...I'm trying real hard.
<snip>

Usenet can be a good pace to get help with this sort of subject, if you
are willing to put some effort in for yourself. But you need to play 'by
the rules' (they aren't really rules, just long established
conventions), for which you need to familiarise yourself with the rules.
Reading this group's FAQ will help a great deal in achieving that.

<URL: http://www.jibbering.com/faq/ >

Richard.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top