473,396 Members | 1,884 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 1619
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Mountain Bikn' Guy | last post by:
How would I do this? public sealed class UtilityClass { public static MyObject Object1;//see note below about importance of static object names in this class public static MyObject Object2;...
3
by: John Haigh | last post by:
I have the need to create an array of objects. Now this sound fairly trivial but I can't figure this out. I have one class called PostingObjectService that has a method GetPostings where the...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
2
by: Mikhail Teterin | last post by:
Hello! I'm going through the fields of a class one at a time and need to handle differently depending on whether they are arrays or scalars. What's the right way to make the distinction? The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.