473,320 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Assign value to control

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
Jul 23 '05 #1
9 2895
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)

Jul 23 '05 #2
cke...@freuse.net 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';
}

Jul 23 '05 #3
Oops...

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

[Friday]

Jul 23 '05 #4
I am newly.

Why not eval...any links?

I tried code, but no luck...

thx

On 15 Apr 2005 20:40:23 GMT, "Evertjan."
<ex**************@interxnl.net> 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")


Jul 23 '05 #5
wrote on 16 apr 2005 in comp.lang.javascript:
On 15 Apr 2005 20:40:23 GMT, "Evertjan."
<ex**************@interxnl.net> 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)

Jul 23 '05 #6
On 15 Apr 2005 15:13:44 -0700, "RobB" <fe******@hotmail.com> 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]


Jul 23 '05 #7
JRS: In article <Xn********************@194.109.133.29>, dated Fri, 15
Apr 2005 20:40:23, seen in news:comp.lang.javascript, Evertjan.
<ex**************@interxnl.net> 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.
Jul 23 '05 #8
Dr John Stockton wrote on 16 apr 2005 in comp.lang.javascript:
JRS: In article <Xn********************@194.109.133.29>, dated Fri, 15
Apr 2005 20:40:23, seen in news:comp.lang.javascript, Evertjan.
<ex**************@interxnl.net> 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)

Jul 23 '05 #9
ck****@refuse.net 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.
Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Mark Kurten | last post by:
i have the code below: for starters the value of txtempid = 1 after i go through this routine (i put a break point on the txtname.value line), the value of txtEmpID is 2 (Just like it is...
2
by: Grey | last post by:
I want to assign a value to HMTL textbox with id "txtABC" in ASP.NET with C#
0
by: JJ_377 | last post by:
The following doesn't assign value to the dropdownlist - WHY? ___________________________________________________________________ In a user control (ascx named USACustomer) : Public Property...
1
by: Marc | last post by:
Hi! I'm working with a C# client that calls a php web service. I've created a wrapper to call the service using .NET wsdl tool (adding a web reference). The call to the server works fine, it...
1
by: annbb | last post by:
I have a piece of code which resets all the control on a form ..but if I also put an option group on the form and the relevant code in the routine to reset it I get an error message saying that...
2
by: Michael Bohman | last post by:
Hi, i have a small problem with assigning a database value to a RadioButtonList control. On my form i have 3 user admin=1, premium=2 and basic=3, theese values is stored in an access database in a...
2
by: Jim McGivney | last post by:
In asp 2.0 I am trying to insert a row using a detailsview control connected to an accessDataSource. I get the error message below. I am having trouble identifing which data field is causing the...
6
by: david | last post by:
I try to use "for" loop to retrieve and assign values in web form. The code is in the following. But it can not be compiled. What I want to do is: txtQ1.Text =...
1
nirmalsingh
by: nirmalsingh | last post by:
hai all, how to take client control value in javascript and assign it to server side control? and i need that server side control value in code behind(c#) to save it. how to do this? Thanx in...
1
by: semomaniz | last post by:
I have a panel set up in a webpage and inside that panel i have various labels and textboxes setup. I am trying to assign a value to the label when the page loads. panel is named cpanel and a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.