create an array of field to test if they are null

S

SAN CAZIANO

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>
 
M

Michael Winter

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
 

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,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top