Help with validation syntax [variable]

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Help with validation syntax [variable]

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Help with validation syntax [variable]

Post 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;
	}
}
(#10850)
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Post 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.
Post Reply