473,698 Members | 2,490 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Value of string???

I have a form with 40 text boxes (default of each = 0). I want to
evaluate each box to see if the value is still zero, and if it is, I
want to add it to a "Zero Count". The box names are "L411 thru L419",
"L421 thru L429", "L431 thru L439" and "L441 thru L449".

Example:

L41 - 002000500
L42 - 000030000
L43 - 000000000
L44 - 000000000

I have construct the control name thru a couple of nested for loops
and all is well (constructed string = "MyForm.L411.va lue").

When I plug this into an If statement to get the value of the control,
everything dies. Here is my If:

for(var j = 1; j < 5; j++)
{
rowString = "L4";

// builds L41
rowString = rowString + j;

for(var x = 1; x < 10; x++)
{
// builds L411
rowString = rowString + x;

// Builds MyForm.L411.val ue
rowString = "MyForm." + rowString + ".value"

//alert shows "MyForm.L411.va lue" - without quotes
//alert(rowString );

if( rowString = "0")
{
zeroCount++:

}
rowString = "L4" + j;
}
}

How can I test the value of a control, based on a string built?

Many tia's

ck
Jul 23 '05 #1
11 1440
ck****@fusert.n et wrote:
I have a form with 40 text boxes (default of each = 0). I want to
evaluate each box to see if the value is still zero, and if it is, I
want to add it to a "Zero Count". The box names are "L411 thru L419",
"L421 thru L429", "L431 thru L439" and "L441 thru L449".

Example:

L41 - 002000500
L42 - 000030000
L43 - 000000000
L44 - 000000000

I have construct the control name thru a couple of nested for loops
and all is well (constructed string = "MyForm.L411.va lue").

When I plug this into an If statement to get the value of the control,
everything dies. Here is my If:

for(var j = 1; j < 5; j++)
{
rowString = "L4";

// builds L41
rowString = rowString + j;

for(var x = 1; x < 10; x++)
{
// builds L411
rowString = rowString + x;

// Builds MyForm.L411.val ue
rowString = "MyForm." + rowString + ".value"
Ooops... you have made 'rowString' the literal string
"MyForm.L411.va lue" which is not what you want to do.

What you want is:

rowString = MyForm.rowStrin g.value;

presuming that the form has an element with a name equal to the value
of 'rowString', i.e. L411.

But this is dangerous, test that the element exists before trying to
get its value, 'cos if it doesn't, your script will die:

if ( MyForm.rowStrin g ) {
rowString = Myform.rowStrin g.value;
} else {
rowString = undefined;
}

which can be abbreviated to:

rowString = ( MyForm.rowStrin g && MyForm.rowStrin g.value );

I would suggest changing the name to rowValue, but that's just me.
Reusing variables this way may make maintenance more difficult, but
again, that's up to you. It may be better to build up the element
name in a variable called 'eleName', then assign the value to
'eleValue' so things are a bit clearer and you'd get:

eleValue = ( MyForm.eleName && MyForm.eleName. value );

//alert shows "MyForm.L411.va lue" - without quotes
//alert(rowString );

if( rowString = "0")
Instead of testing whether the value of 'rowString' is equivalent to
the string '0', you are assigning it the value of the string '0'.

As a hint always write such tests:

if( '0' = rowString )

That way you will get a script error rather than just erroneous
results. The correct syntax (assuming you want to test for equality)
is:

if( '0' == rowString )
{
zeroCount++:

}
rowString = "L4" + j;
}
}

How can I test the value of a control, based on a string built?

Many tia's

ck

--
Rob
Jul 23 '05 #2
ck****@fusert.n et wrote:
I have a form with 40 text boxes (default of each = 0). I want to
evaluate each box to see if the value is still zero, and if it is, I
want to add it to a "Zero Count". The box names are "L411 thru L419",
"L421 thru L429", "L431 thru L439" and "L441 thru L449".


(That are 36, not 40, text inputs).

var i, j, nZeroCount, oForm, oCurElem, oCurVal;
oForm = document.forms['MyForm'];
nZeroCount = 0;
for (i=1; i<10; i++) {
for (j=1; j<5; j++) {
oCurElem = oForm.elements["L4" + j + i];
if (oCurElem) {
oCurVal = oCurElem.value;
if (!isNaN(oCurVal ) && parseInt(oCurVa l, 10) == 0) {
nZeroCount++;
}
}
}
}

ciao, dhgm
Jul 23 '05 #3
> When I plug this into an If statement to get the value of the control,
everything dies. Here is my If: if( rowString = "0")


If you had used JSLint, it would have alerted you to this statement. You
probably meant

if (rowString == "0") {

See http://www.JSLint.com
Jul 23 '05 #4
Thank you very much! That got me on the right track now! :)

Although, if I use basically the same loop earlier in the structure,
how can I assign a "0" to non-numeric entries? Granted what I'm
checking for (below) is null, but I don't think that is exactly what
I want to do. I believe I should be checking for "non-numeric"
instead.

This is what I am trying, but it doesn't work :(

for(var j = 1; j < 5; j++)
{
rowString = "L4";
rowString = rowString + j;

for(var x = 1; x < 10; x++)
{
rowString = rowString + x;
if (eval ("document.form s[0]." + rowString + ".value") ==
Null)
{
MyForm.rowstrin g.value = 0;
}
rowString = "L4" + j;
}
}

ck
Jul 23 '05 #5
ck****@refuse.n et wrote:
Thank you very much! That got me on the right track now! :)

Although, if I use basically the same loop earlier in the structure,
how can I assign a "0" to non-numeric entries? Granted what I'm
checking for (below) is null, but I don't think that is exactly what
I want to do. I believe I should be checking for "non-numeric"
instead.

This is what I am trying, but it doesn't work :(

for(var j = 1; j < 5; j++)
{
rowString = "L4";
rowString = rowString + j;
Save yourself some typing, two lines to one:

rowString = "L4" + j;

for(var x = 1; x < 10; x++)
{
rowString = rowString + x;
or

rowString += x;

Since rowString is a string, x will be concatenated (i.e. appended)
to the string.


if (eval ("document.form s[0]." + rowString + ".value") ==
Null)


Ditch 'eval', it is nearly never, ever needed.

The value of an empty input is '' (empty string), not 'null' (note
captialisation) .

I'll assume that what you want to do is see if the value is zero. If
so, add it to the zero count. If it's only digits, leave it alone.
If it's anything else, give an error. So here goes:

// Mentioned sensible variable names before
var eleValue;

// Make sure the element exists, then get its value
if ( document.forms[0].rowString
&& ( eleValue = document.forms[0].rowString.valu e )) {

// If the value is zero, add one to zeroCount
if ( eleValue == 0 ){
zeroCount++;

// If there is anything in the value that isn't 0-9
} else if ( /\D/g.test(eleValue ) ) {
// Do whatever needs to be done if there are
// things in the value that aren't digits
}
}
[...]

--
Rob
Jul 23 '05 #6
JRS: In article <42625d97$0$923 0$5a62ac22@per-qv1-newsreader-
01.iinet.net.au >, dated Sun, 17 Apr 2005 22:56:17, seen in
news:comp.lang. javascript, RobG <rg***@iinet.ne t.auau> posted :

Ditch 'eval', it is nearly never, ever needed.


Yes and no. One rarely needs to write it; OTOH, it and function call
are probably the two statements that I execute most frequently.

<URL:http://www.merlyn.demo n.co.uk/js-quick.htm>; View Source, or enter
remoteEval.toSt ring()
in the textarea and press RmEv !


BTW, could someone(s) look at
<URL:http://www.merlyn.demo n.co.uk/js-boxes.htm>
with non-MSIE to see if all is reasonably well?

The main thing is the box starting -- New Code : in which ShoTrim
will I hope be shown as containing the string "' comment ? '" and
ShoLinCnt's first line should be the only lone that wraps and a line
-- One visible blank line next; scroll for more code :
should be true. And function NotALot should fit the next box.

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #7
Dr John Stockton wrote:
JRS: In article <42625d97$0$923 0$5a62ac22@per-qv1-newsreader-
01.iinet.net.au >, dated Sun, 17 Apr 2005 22:56:17, seen in
news:comp.lang. javascript, RobG <rg***@iinet.ne t.auau> posted :
Ditch 'eval', it is nearly never, ever needed.
Yes and no. One rarely needs to write it; OTOH, it and function call
are probably the two statements that I execute most frequently.

<URL:http://www.merlyn.demo n.co.uk/js-quick.htm>; View Source, or enter
remoteEval.toSt ring()
in the textarea and press RmEv !


BTW, could someone(s) look at
<URL:http://www.merlyn.demo n.co.uk/js-boxes.htm>
with non-MSIE to see if all is reasonably well?


Gosh, nearly missed you way down here!

The main thing is the box starting -- New Code : in which ShoTrim
will I hope be shown as containing the string "' comment ? '" and
Nope, does not appear. Comparison with IE shows it to be totally
missing in Firefox.
ShoLinCnt's first line should be the only lone that wraps and a line
The comment that causes the line to wrap in IE is not there in
Firefox.
-- One visible blank line next; scroll for more code :
should be true.
Yes, but the IE code is missing a few linefeeds after semi-colons
that are in Firefox and comments vanish:

FF:

function ShoDoo(Fn) {
var St = ShoTrim(Fn.toSt ring());
Depict(BoxX, ShoLinCnt(St, BoxX), St, "red");
Fn();
return "";
IE:

function ShoDoo(Fn) { // N.B. this calls Fn() - ADD show others ?
var St = ShoTrim(Fn.toSt ring())
Depict(BoxX, ShoLinCnt(St, BoxX), St, "red") ; Fn() ; return "" }

And function NotALot should fit the next box.


FF:

function NotALot() {
}

Yup, that's not a lot!! The comments show up in Opera though.

:-)

In general, Opera does it all differently again - dropping some
comments, adding in new lines and dropping others...
--
Rob
Jul 23 '05 #8
RobG wrote:
Ditch 'eval', it is nearly never, ever needed.

[snip]

Hi, I don't mean to hijack the thread to start beating a dead horse,
but I can't get your eval replacement code to work. I think I
implemented it correctly in the sayHi() function. eleValue is always
returned undefined, any suggestions?

thanks

-- brian

<html>
<head>
<script type="text/javascript">

function sayHi() {
var eleValue;
var rowString = "txt1";
if ( document.forms[0].rowString && ( eleValue =
document.forms[0].rowString.valu e )) {
alert("hello");
} else {
alert(eleValue) ;
}
}

function sayHi2() {
var rowString = "txt1";
res = eval("document. forms[0]." + rowString + ".value");
alert(res);
}

</script>
</head>
<body>

<form name="main">
<input type="text" name="txt1" size="10" value="hi" />
<input type="button" onclick="sayHi( );" />
</form>
</body>
</html>

Jul 23 '05 #9
Brian Munroe wrote:
var rowString = "txt1";
if ( document.forms[0].rowString && ( eleValue =
document.forms[0].rowString.valu e )) {
alert("hello");
} else {
alert(eleValue) ;
}
}


var rowString = "txt1";
if (document.forms[0][rowString]
&& (eleValue = document.forms[0][rowString].value)
) {
alert("hello");
}
else {
alert(eleValue) ;
}

ciao, dhgm
Jul 23 '05 #10

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

Similar topics

10
13733
by: sam | last post by:
Hi, I m wondering why I can't declare reference variable for default value in a function argument variable? eg. class A { void f(string &str="");
1
16647
by: LRW | last post by:
Below is some of the code I'm using. I have a PHP page generating a list of items. I've made it so that each one has a radiobutton with a unique value. You can click on the radiobutton and it will change the value of a text field to the value of the radiobutton--all that works fine! Now, I need to also have it populate a textfield with the value of a textfield in the row that's selected. I can make unique field names, no problem. I...
8
2791
by: Grant Wagner | last post by:
I'm a bit confused by String() (typeof 'string') vs new String() (typeof 'object'). When you need to access a method or property of a -String-, what type is JavaScript expecting (or rather, what should you provide), a -String object- or a -string-? Given the following benchmark: var t = (new Date()).getTime(); for (var ii = 0; ii < 100000; ++ii) { var x = (String('hi')).charAt(0); // typeof 'string'
6
2195
by: Webgour | last post by:
How to go from string "abc" to string "a|b|c"?
6
2816
by: Chris Simmons | last post by:
I know that a String is immutable, but I don't understand why this piece of code fails in nUnit: // BEGIN CODE using System; class Test { public static void Main( String args )
5
1666
by: Steven Blair | last post by:
I have the following string: <CardDetails>4561</CardDetails><Message Identifier>1</Message Identifier><TID>88888888</TID> I need to be able to extract the values from the tags easily in C#. Being able to search through this string at any point for a partiuclar tag would be useful. Can anyone suggest the best / most effecient way to achieve this?
10
8601
by: Zontar | last post by:
I'm trying to improve performance on a query, and I was wondering if this is possible in Access. Let's say I have a table with one text column and one row. In that column, I have a field name from another table . This fieldtable gets updated from a form, but it never has more than one row in it. So an example value in my look like this: .
0
4361
by: Nick Valeontis | last post by:
code: ---------------- ---------------- ---------------- -------------------------------- ---------------- namespace TestApp { public partial class Form1 : Form { private object s = "1"; public Form1() { InitializeComponent();
6
2918
by: Andrus | last post by:
I need to create generic table field level cache. Table primary key (PrimaryKeyStructType) can be int, string or struct containing int and string fields. FieldName contains table field name to be cached. Remove() should remove table row from cache. Row is identified by table primary key. Trying compile class causes error Operator '==' cannot be applied to operands of type
2
2601
by: sumanta123 | last post by:
Dear Sir, In my develpment i am getting stuck for a senario.Kindly please help me for it. Everytime we give the request we get the response of 8 records and its corresponding value. Then next button will display more and more records if it is availbale in data base. I am attachcing the screen shoot of my problem CMP is a column in the this record.
0
8685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8880
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7743
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4373
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4625
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2342
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.