Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

isArray function

Status
Not open for further replies.

rico14d

Programmer
Apr 8, 2002
135
GB
Hi,

I need a function to check for an array of form elements.
At the moment I am using this -

if (document.form1.text_box.length == undefined)
//not array

But i need a way to do this in IE 5.01 and it doesnt seem to work.

Anyone have a different way of checking for this?

Thanks,
Rich
 
Here you go..
Code:
<script language=javascript>
<!--//
function fieldValidate() {
	var d = document.theForm;

	var isFull = fieldCheck(
	d.field1.value, "FIELD #1",
	d.field2.value, "FIELD #2",
	d.field3.selectedIndex, "FIELD #3");

	if (!isFull) {
		return false;
	}
}

function fieldCheck() {
	for (var i = 0; i < arguments.length; i += 2) {
		if (!arguments[i]) {
			alert ("Please enter " + arguments[i + 1] + ".");
			return false;
		}
	}
	return true;
}
//-->
</script>

<form method="POST" onSubmit="fieldCheck()">
FIELD 1: <input type="text" name="field1"><br>
FIELD 2: <input type="text" name="field2"><br>

FIELD 3:
<select name="FIELD3">
<option value=""></option>
<option value="Test"></option>
</form>

 
Oops.. forgot something
Code:
<select name="[b]field3[/b]">
<option value=""></option>
<option value="Test"></option>
[b]</select>[/b]

 
Get an array of same-name elements using document.getElementsByName(). It should return null if none found.

Adam
 
Hi,

Thanks for your help, i actually got it working by changing the code slightly -
Code:
if (document.form1.text_box.length == undefined)

if (typeof(document.form1.text_box.length) == 'undefined')
I think the quotes around the "undefined" made the difference.

Cheers,
Rich.
 
If you wanted to check for the existence of just one box, you could use this too:
Code:
if(document.form1.text_box)

Adam
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top