"Randell D." <reply.to.news.group.only@and.share.com> writes:
[color=blue]
> 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.[/color]
Ok, that should be fairly simple.
[color=blue]
> 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>[/color]
[color=blue]
> 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";[/color]
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.
[color=blue]
> 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?[/color]
[color=blue]
> In PHP I could do something like :
> foreach($requiredFields as $inputTagName=>$realFieldName)
> { print("<br>Input tag $inputTagName is known by the user as
> $realFieldName"); }[/color]
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)
[color=blue]
> 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...[/color]
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 -
lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'