Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Assign value to control

Reply
Thread Tools

Assign value to control

 
 
ckerns@freuse.net
Guest
Posts: n/a
 
      04-15-2005
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


 
Reply With Quote
 
 
 
 
Evertjan.
Guest
Posts: n/a
 
      04-15-2005
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")


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

 
Reply With Quote
 
 
 
 
RobB
Guest
Posts: n/a
 
      04-15-2005
(E-Mail Removed) wrote:
> 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[i]])
&& /^\D*$/.test(el.value))
el.value = '0';
}

 
Reply With Quote
 
RobB
Guest
Posts: n/a
 
      04-15-2005
Oops...

if ((el = els[cnames[i]])
&& !/^\d+$/.test(el.value))

[Friday]

 
Reply With Quote
 
ckerns@furssse.net
Guest
Posts: n/a
 
      04-15-2005
I am newly.

Why not eval...any links?

I tried code, but no luck...

thx

On 15 Apr 2005 20:40:23 GMT, "Evertjan."
<(E-Mail Removed)> wrote:

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


 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      04-16-2005
wrote on 16 apr 2005 in comp.lang.javascript:
> On 15 Apr 2005 20:40:23 GMT, "Evertjan."
> <(E-Mail Removed)> wrote:
>
>> 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.

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

 
Reply With Quote
 
ckerns@refuse.net
Guest
Posts: n/a
 
      04-16-2005
On 15 Apr 2005 15:13:44 -0700, "RobB" <(E-Mail Removed)> wrote:

>Oops...
>
> if ((el = els[cnames[i]])

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]


 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      04-16-2005
JRS: In article <Xns9639E6A64657Deejj99@194.109.133.29>, dated Fri, 15
Apr 2005 20:40:23, seen in news:comp.lang.javascript, Evertjan.
<(E-Mail Removed)> posted :
>
>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.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      04-16-2005
Dr John Stockton wrote on 16 apr 2005 in comp.lang.javascript:

> JRS: In article <Xns9639E6A64657Deejj99@194.109.133.29>, dated Fri, 15
> Apr 2005 20:40:23, seen in news:comp.lang.javascript, Evertjan.
> <(E-Mail Removed)> posted :
>>
>>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.


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

 
Reply With Quote
 
Richard Cornford
Guest
Posts: n/a
 
      04-17-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> RobB wrote:

<snip>
>> if ((el = els[cnames[i]])

> 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[i]]) && /^\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[i]]) && /^\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[i]])

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[i]])

- 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[i]] - 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[i] -. 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[i] - 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem how to assign value to object list control chintinfong@gmail.com ASP .Net Mobile 0 06-12-2007 04:19 AM
assign control value from javascript soni2926@yahoo.com ASP .Net 1 04-25-2007 09:42 PM
User Control Dropdownlist: can't assign value to JJ_377@hotmail.com ASP .Net 0 07-22-2005 12:17 AM
Assign value to HTML control Grey ASP .Net 2 05-20-2004 10:32 AM
Re: Assign nulll value to an Integer Boban Dragojlovic ASP .Net 1 07-24-2003 09:22 PM



Advertisments