By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,963 Members | 1,815 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,963 IT Pros & Developers. It's quick & easy.

using for to read an array with string indices

P: n/a

Folks,

I can program fairly comfortably in PHP and can, for the most part using
these skills and others that I've picked up over the years manage to
read/understand most code in Javascript... so I'm just asking for a few
pointers (or the full solution if you have the time) for what I want to do.

Basically, I want to write a javascript wherby I only need to pass it the
names of form fields - then my javascript will check each form field has a
value greater than zero in length - Any field value that fails this test is
noted... when all fields have been tested, an alert box opens telling the
user that they forgot to fill in 'x' number of fields and the field names
are named as they would appear visably on the form. I can write most of the
script but there is a method that I would like to do but don't know how to
implement it in Javascript...

A brief example of a form field would be the following HTML:

Street Name: <input type=text size=30 name=streetName>
My solution to alert the user in a user friendly manner would be to have an
array with string indices. Thus the following piece of javascript:

var requiredfields=new Array();
requiredfields['streetName']="Street Name";
The reason why I have it setup like this is I can use the index of an
element to check a specific field in a form. If that field string length is
zero, then the alert box will report "Street Name" and not "streetName"...
My question is: How can I create a loop that will give me the element name?
In PHP I could do something like :

foreach($requiredFields as $inputTagName=>$realFieldName)
{ print("<br>Input tag $inputTagName is known by the user as
$realFieldName"); }

How can I achieve this in Javascript? How can I loop thru a string indexed
array? It does not have to be a for loop... but I do want to have the real
value of the array element, and the array element value...

Can anybody help me out? I'm still playing with the rest of my scrpit - I'm
confident that I can do the rest...

Thanks in advance, replies please via the newsgroup... it might help someone
else...
randell d.
Jul 20 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
"Randell D." <re**********************@and.share.com> writes:
Basically, I want to write a javascript wherby I only need to pass it the
names of form fields - then my javascript will check each form field has a
value greater than zero in length - Any field value that fails this test is
noted... when all fields have been tested, an alert box opens telling the
user that they forgot to fill in 'x' number of fields and the field names
are named as they would appear visably on the form.
Ok, that should be fairly simple.
I can write most of the script but there is a method that I would
like to do but don't know how to implement it in Javascript...

A brief example of a form field would be the following HTML:

Street Name: <input type=text size=30 name=streetName> My solution to alert the user in a user friendly manner would be to have an
array with string indices. Thus the following piece of javascript:

var requiredfields=new Array();
requiredfields['streetName']="Street Name";
Use "new Object()". The only difference between objects and arrays is
related to integer-indices. If you don't use such, you don't need
an array.
The reason why I have it setup like this is I can use the index of an
element to check a specific field in a form. If that field string length is
zero, then the alert box will report "Street Name" and not "streetName"...
My question is: How can I create a loop that will give me the element name? In PHP I could do something like :
foreach($requiredFields as $inputTagName=>$realFieldName)
{ print("<br>Input tag $inputTagName is known by the user as
$realFieldName"); }
In Javascript, the equivalent code would be:
---
for (inputTagName in requiredFields) {
document.write("<br>Input tag " + inputTagName +
" is known by the user as " +
requiredFields[inputTagName]);
}
---
(works the same whether you use Array or Object)
How can I achieve this in Javascript? How can I loop thru a string indexed
array? It does not have to be a for loop... but I do want to have the real
value of the array element, and the array element value...


The construction:
for (index in object) {...}
will let the variable "index" iterate through the properties of the
object "object". You can then access the property value as "object[index]".

For this purpose, arrays are just objects.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2

P: n/a
"Randell D." <re**********************@and.share.com> wrote in
news:ypQvb.462767$6C4.34453@pd7tw1no:
My solution to alert the user in a user friendly manner would be to
have an array with string indices. Thus the following piece of
javascript:

var requiredfields=new Array();
requiredfields['streetName']="Street Name";
In Javascript, "associative arrays" are implemented by Object rather than
Array. So make that first line:

var requiredfields=new Object();

The second statement will work, but you're going to be adding several
fields, so look up "object literals" in a good JS reference to learn a way
to do it with much less typing, as in:

var requiredfields = {
streetname:"Street Name",
city:"City",
state:"State or Province"
};
The reason why I have it setup like this is I can use the index of an
element to check a specific field in a form. If that field string
length is zero, then the alert box will report "Street Name" and not
"streetName"... My question is: How can I create a loop that will give
me the element name? In PHP I could do something like :

foreach($requiredFields as $inputTagName=>$realFieldName)
{ print("<br>Input tag $inputTagName is known by the user as
$realFieldName"); }

How can I achieve this in Javascript? How can I loop thru a string
indexed array? It does not have to be a for loop... but I do want to
have the real value of the array element, and the array element
value...


Use the "for (<variable> in <object>)" syntax:

for (var inputTagName in requiredfields) {
document.write("<br>Input tag "+inputTagName+
" is known by the user as "+requiredfields[inputTagName]);
}
Jul 20 '05 #3

P: n/a
"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote:
The only difference between objects and arrays is related to

integer-indices.

And length property, isn't that right?
Jul 20 '05 #4

P: n/a
"Vjekoslav Begovic" <vj*******@inet.hr> writes:
"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote:
The only difference between objects and arrays is related to integer-indices.


Your news-client isn't quoting correctly!
And length property, isn't that right?


Yes, but that is also very much *related* to integer indices.

More precisely:

Arrays are objects with extra functionality.

The Array.prototype object contains a number of methods that apply to
objects with a numeric length property and properties with integer
indices. Examples: push, join, concat. None of these require the object
to be an array.

The length property is "magic" (not something that can be programmed in
Javascript itself). It is always at least one larger than the largest
integer property of the object. Setting a larger integer named property
increases the length. Setting the length lower removes properties.

All in all, it is only about integer named properties. Even the length
property is linked to these.

This code shows how arrays work:
---
function arrayInfo(arr) {
var indices = [];
var maxIndex = -1;
for (var i in arr) {
if (i > maxIndex) {maxIndex = i;}
indices.push(i);
}
return "Length: " + arr.length +
" Maximal index: " + maxIndex +
" Indicies: {" + indices.join(",") + "}";
}

var x = [];
alert(arrayInfo(x)); // length:0, maxIndex: -1, indicies {}
x.length = 20;
alert(arrayInfo(x)); // length:20, maxIndex: -1, indicies {}
x[25] = 42;
alert(arrayInfo(x)); // length:26, maxIndex: 25, indicies {}
delete x[25];
alert(arrayInfo(x)); // length:26, maxIndex: -1, indicies {}
---

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #5

P: n/a

"Eric Bohlman" <eb******@earthlink.net> wrote in message
news:Xn*******************************@130.133.1.4 ...
"Randell D." <re**********************@and.share.com> wrote in
news:ypQvb.462767$6C4.34453@pd7tw1no:
My solution to alert the user in a user friendly manner would be to
have an array with string indices. Thus the following piece of
javascript:

var requiredfields=new Array();
requiredfields['streetName']="Street Name";


In Javascript, "associative arrays" are implemented by Object rather than
Array. So make that first line:

var requiredfields=new Object();


Me thinks the two books I do have are too old (or not written clear
enough)... I have read, and understand a bit on javascript objects - its
something I thought I understood but I guess I'm wrong. A good javascript
book will be on my Christmas list this year I think...
Jul 20 '05 #6

P: n/a

"Randell D." <re**********************@and.share.com> wrote in message
news:ypQvb.462767$6C4.34453@pd7tw1no...

Folks,

I can program fairly comfortably in PHP and can, for the most part using
these skills and others that I've picked up over the years manage to
read/understand most code in Javascript... so I'm just asking for a few
pointers (or the full solution if you have the time) for what I want to do.
Basically, I want to write a javascript wherby I only need to pass it the
names of form fields - then my javascript will check each form field has a
value greater than zero in length - Any field value that fails this test is noted... when all fields have been tested, an alert box opens telling the
user that they forgot to fill in 'x' number of fields and the field names
are named as they would appear visably on the form. I can write most of the script but there is a method that I would like to do but don't know how to
implement it in Javascript...

A brief example of a form field would be the following HTML:

Street Name: <input type=text size=30 name=streetName>
My solution to alert the user in a user friendly manner would be to have an array with string indices. Thus the following piece of javascript:

var requiredfields=new Array();
requiredfields['streetName']="Street Name";
The reason why I have it setup like this is I can use the index of an
element to check a specific field in a form. If that field string length is zero, then the alert box will report "Street Name" and not "streetName"...
My question is: How can I create a loop that will give me the element name? In PHP I could do something like :

foreach($requiredFields as $inputTagName=>$realFieldName)
{ print("<br>Input tag $inputTagName is known by the user as
$realFieldName"); }

How can I achieve this in Javascript? How can I loop thru a string indexed array? It does not have to be a for loop... but I do want to have the real
value of the array element, and the array element value...

Can anybody help me out? I'm still playing with the rest of my scrpit - I'm confident that I can do the rest...

Thanks in advance, replies please via the newsgroup... it might help someone else...
randell d.


Thanks to all who replied / I'll have to invest more time in my javascript -
my books are at least two years old so that might be where I'm getting my
knickers in a twist...

I'll play a little more...

Thanks again,
randelld
Jul 20 '05 #7

P: n/a
"Randell D." <re**********************@and.share.com> writes:
Me thinks the two books I do have are too old (or not written clear
enough)...
Very likely :)
I have read, and understand a bit on javascript objects - its
something I thought I understood but I guess I'm wrong.
Douglas Crockford has a good summary here:
<URL:http://www.crockford.com/javascript/survey.html>
A good javascript book will be on my Christmas list this year I
think...


I never had a good Javascript book myself, but the FAQ does recommend
one. <URL:http://jibbering.com/faq/#FAQ3_1>

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #8

P: n/a
> Me thinks the two books I do have are too old (or not written clear
enough)... I have read, and understand a bit on javascript objects - its
something I thought I understood but I guess I'm wrong. A good javascript
book will be on my Christmas list this year I think...


Nearly all JavaScript books are horribly awful. Get a good one: Flanagan's
Definitive Guide, 4th edition, from O'Reilly. Don't wait for Christmas.

http://www.crockford.com/javascript/javascript.html

Jul 20 '05 #9

P: n/a
JRS: In article <ypQvb.462767$6C4.34453@pd7tw1no>, seen in
news:comp.lang.javascript, Randell D. <re**********************@and.share
..com> posted at Sat, 22 Nov 2003 21:19:26 :-
Basically, I want to write a javascript wherby I only need to pass it the
names of form fields - then my javascript will check each form field has a
value greater than zero in length - Any field value that fails this test is
noted... when all fields have been tested, an alert box opens telling the
user that they forgot to fill in 'x' number of fields and the field names
are named as they would appear visably on the form.


That is similar to a subset of a situation discussed here recently; see

Subject: Re: Phone Validation Problem (I reformatted the code to make
it easier to read)
Date: Fri, 21 Nov 2003 22:11:14 +0000
Message-ID: <Kz**************@merlyn.demon.co.uk>

The code given there is now obsolete; the current version (embodying
suggested improvements) is at <URL:http://www.merlyn.demon.co.uk/jt.htm>.
Since all your fields are to be tested similarly, you could possibly
simplify the code a little, for instance by supplying a (constant)
variable instead of unchanging literals.

The question of seeking all that is to be tested has, I think, been
adequately answered.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.