Page 1 of 1

Help with validation syntax [variable]

Posted: Sun Mar 25, 2007 12:30 am
by facets
Hey all,

I need to write [variable] into validation code where the variable is a number being incremented by a loop. The braces are not being used an array, so i'm probably breaking some syntax code.. Can this be done? The following code errors :

Error: syntax error
var myindex=document.forms[0].driverType[(+validateCount+)].selectedIndex;

Code: Select all

var validateCount = 0;
function checkForm() {
var myindex=document.forms[0].driverType[(+validateCount+)].selectedIndex;
if (myindex == "") {
	alert("\n You must make a selection from the drop-down menu.");

	return false;
	}
}
If I write my variable directly

Code: Select all

var myindex=document.forms[0].driverType[0].selectedIndex;
It tells me that

Code: Select all

Error: document.forms[0].driverType has no properties
Does anyone have any suggestions?

tia, Will

Re: Help with validation syntax [variable]

Posted: Sun Mar 25, 2007 1:28 am
by Christopher
Not exactly sure what you are trying to do, but using a variable as an array index it would be:

Code: Select all

var validateCount = 0;
function checkForm() {
var myindex=document.forms[0].driverType[validateCount].selectedIndex;
if (myindex == "") {
	alert("\n You must make a selection from the drop-down menu.");

	return false;
	}
}

Posted: Sun Mar 25, 2007 3:00 am
by facets
This code throws an error :

Code: Select all

var myindex=document.forms[0].driverType[validateCount].selectedIndex; 
Error: document.forms[0].driverType has no properties
Source File: http://10.1.1.2/market/final_new_backup.php
Line: 17

My php code writes this to the browser :

Code: Select all

<select name="driverType[0]">
So I need the JS to write driverType[variable] rather than it think it's an array.
Does this make sense? I've been told that this is probably bad coding as [] are reserved characters.