using for to read an array with string indices

R

Randell D.

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.
 
E

Eric Bohlman

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]);
}
 
L

Lasse Reichstein Nielsen

Randell D. said:
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
 
V

Vjekoslav Begovic

Lasse Reichstein Nielsen said:
The only difference between objects and arrays is related to
integer-indices.

And length property, isn't that right?
 
L

Lasse Reichstein Nielsen

Vjekoslav Begovic said:
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 {}
 
R

Randell D.

Eric Bohlman said:
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...
 
R

Randell D.

Randell D. said:
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
 
L

Lasse Reichstein Nielsen

Randell D. said:
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:
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
 
D

Dr John Stockton

JRS: In article <ypQvb.462767$6C4.34453@pd7tw1no>, seen in
Randell D. <[email protected]
..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: <[email protected]>

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.
 
D

Douglas Crockford

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
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top