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 Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dynamic multiple select fields with the same name need validation

Status
Not open for further replies.

justride

Programmer
Joined
Jan 9, 2004
Messages
251
Location
US
I have n amount of select html fields

select[0][] select[1][] select[n][]....

I need pass the first oneto a jscript function to verify data is present. How can I do this?

I keep getting java script errors when I pass select[0][].

Thanks
 
Show us the code you have built to date. We can't do much with what you have posted so far.

I don't know why you are referring to the items as somename[n][] since the last set of brackets appear empty and so have no meaning.

Cheers,
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
And also, what is "select"? Is it a name for an array you have? Where is it defined? Is it pseudo-code and just an example?

You need to explain these things. We can't give you anything to go on if you don't clearly explain the details you give us.

If "select" is an array, then I would have thought this would work:

Code:
select[0]

i.e. without the last set of square brackets (which as Jeff says, are empty and meaningless in your example above).

Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Sorry for the confusion. I understand the second array index is useless however for my php session variables this is the only way I got it to work, I forget the issue I was having.

Anyways, here is the selects I was talking about

<select name="requirements[0][]" multiple size=10>

<select name="requirements[1][]" multiple size=10>

<select name="requirements[n][]" multiple size=10>

when the form gets submiited a verify jscript function gets called, I need to pass <select name="requirements[0][]" multiple size=10> and thats it, I just need to make sure at least one element in the first select is "selected"

Thanks
 
With the following HTML:
Code:
<form name="myForm" action="" onsubmit="return(validate());">
 <select name="requirements[0][]" multiple size=10>
  <option>1</option><option>2</option><option>3</option>
  <option>11</option><option>12</option><option>13</option>
 </select>
 <input type="submit"/>
</form>
Then you can use this javascript function to do what you want:
Code:
<script type="text/javascript">
function validate() {
	var b_valid = true;
	if (document.forms['myForm'].elements('requirements[0][]').selectedIndex == -1) b_valid = false;
	if (!b_valid) alert('Error validating');
	return b_valid;
}
</script>
You can easily add extra validation tests to this... just set the value of b_valid to false at any stage to force it to "fail" and not submit the form.

Cheers,
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
I'll give that a shot.

Thanks,
Chris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top