| re: create an array of field to test if they are null
On Wed, 06 Oct 2004 14:13:21 GMT, SAN CAZIANO <alberto.kalb@tin.it> wrote:
[color=blue]
> is there a function that get the name of the first input field of the
> current form ?[/color]
No, but you can use the elements collection to get form controls by
ordinal number:
formObj.elements[0] // first element
formObj.elements[1] // second, etc...
and from that you can get the name or id:
formObj.elements[0].name or .id
However, that isn't really necessary for what you (seem to) want to do.
[color=blue]
> in my example below I want create an array of form field name and in the
> onsubmit assign all element's name to create a simple iteration to test
> if some elements in my array, that must be required, are null:
> something like function verify(array of string) and in onsubmit
> something like return onsubmit(field1,field2,field3....)[/color]
If you use
<form ... onsubmit="return verify(this)">
function verify(form) {
you can use the elements collection, as I demonstrated above.
To check that the first control has a value:
function verify(form) {
if('' == form.elements[0].value) {
alert('Please enter a value');
return false;
}
}
If you want to actually use the name of the control, substitute the number
with a string containing that name:
if('' == form.elements['myName'].value) {
[snip]
[color=blue]
> <SCRIPT LANGUAGE=JAVASCRIPT>[/color]
Don't use the language attribute any more. Not only is it deprecated, but
the required type attribute makes it redundant.
<script type="text/javascript">
[snip]
I hope that helps,
Mike
--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail. |