View Full Version : multiple field elements check

10-10-2003, 02:30 PM
I get N fields (N is unknown) named as follow:

--> It's generated from server side...

And I want to run a validity check on those fields before I submit.
The problem is that I can't tell how many fields I have, and even if so, I don't know how to query about each field, since their names are changing from one to another.

Can anyone help please?

Ofcourse I can make my server side script, write the JS script with check for each field. But I'm looking for the client side function.

Roy Sinclair
10-10-2003, 02:50 PM
Each form object has an "elements" array associated with it which you can use to scan through the whole form and find those fields regardless of how many actually exist on the form.

document.forms["formname"].elements[x] -- where x is a number or a literal field name.

10-10-2003, 02:55 PM
You can get the number of elements in a form by doing this:
var frmlen=document.forms[0].length

"forms[0]" would be the first form in your document. You could then use this variable to loop through the form elements like this:
{do some validating}
{do some validating}
etc ......

10-10-2003, 02:59 PM
thank you both
but those are not the ONLY fields in the form, so I can't know what number of element to start from, and where to stop the checking...

Roy Sinclair
10-10-2003, 03:02 PM
Requestcode already showed you the code for iterating through the elements array. The elements array contains object references to each form element. That means you can check all kinds of information about each form element including the name of that element. With the name you can simply check to see if the name matches the pattern you're expecting and then perform whatever validation you want on fields which match.

10-10-2003, 03:07 PM
Thanks :)

Another question if I already posted...
When I put DISABLED in a form field, it becomes un-accessable and is also ignored when I submit the form.

Is there a way to only make it not accessable (yet visible) and not ignoring it upon submission?

Roy Sinclair
10-10-2003, 03:35 PM
Yes, replace DISABLED with READONLY.


10-10-2003, 03:50 PM
that's it.
Is there something parallel to READONLY for checkboxes and radio buttons?


10-10-2003, 04:18 PM
If READONLY does not work then try this: onFocus="this.blur()" if you don't want someone to change them.

10-10-2003, 04:36 PM
it's quite the same
since blur, comes after click
and it's possible to change checkboxes and radios....

Any other idea?

10-10-2003, 05:01 PM
This disables a radio button:
<input type="radio" name="r1" value="Radio1" disabled>

If you want to disable the above using javascript then it would be:
and to enable it:

10-10-2003, 05:23 PM
read above!
When I use DISABLED, the radio is ignored on submission!
I don't want to ignore it, just don't want the user to change it!

But I have an idea!!!
Is there a way to reset (<input type="reset">) only part of the form?

Roy Sinclair
10-10-2003, 10:47 PM
Since you already have the code for iterating through the form why not use it AFTER you've made all the other checks on the form to check for form elements that are checkboxes or radio buttons and are disabled and change them from disabled to enabled.

Of course since you may have customers with Javascript enabled you're going to have to handle the case where the elements don't come back anyway so I'd just skip the whole process since your server side code is going to have to cope with elements not returning anyway. (Not to mention the fact that you should never accept and update data if you send it to a user as read-only).

10-11-2003, 12:49 AM