468,539 Members | 1,575 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,539 developers. It's quick & easy.

create an array of field to test if they are null

is there a function that get the name of the first input field of the
current form ?
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....)

<HTML>
<HEAD>
<TITLE>Confirm Dialog Test</title>

<SCRIPT LANGUAGE=JAVASCRIPT>
function verify()
{
if(document.forms[0].GetElementById[0].value=="")
{
alert("Please enter a value in the field");
return false;
}
else{
return true;
}
}
</SCRIPT>
</HEAD>
<BODY>
<FORM onSubmit="return verify()">
Name: <INPUT TYPE=TEXT NAME="myName"><BR>
<INPUT TYPE=SUBMIT VALUE="Submit">
</FORM>
</BODY>
</HTML>
Jul 23 '05 #1
1 1425
On Wed, 06 Oct 2004 14:13:21 GMT, SAN CAZIANO <al**********@tin.it> wrote:
is there a function that get the name of the first input field of the
current form ?
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.
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....)
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]
<SCRIPT LANGUAGE=JAVASCRIPT>


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.
Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by John Haigh | last post: by
2 posts views Thread by Mikhail Teterin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.