...

View Full Version : Repeat region validation



Nik36
12-07-2006, 05:17 PM
I have a number of dynamically generated drop down menus in an ASP repeat region. The output is:

myorder1
myorder2
myorder3

and so on. The values are set to match the specific data record value.

I want to validate these menus so that a user can't select the same value for any one of the drop downs.

At the moment I'm trying to get the Js to return the values for each instance of the myorder drop down:

function checkDupes(myform) {

var i=1
for (i=1;i<=4;i++)
{
document.write(document.myform.myorder+i.selectedIndex
document.write("<br />")
}

}

I just get a NAN output.

nikkiH
12-07-2006, 11:39 PM
If your form is named "myform" and your select element is named "myorder" then you reference it as

document.myform.myorder.options[document.myform.myorder.selectedIndex].value;

as a shortcut.

If you're trying to loop through elements with a name like myorder1, myorder2, etc, you have to use the long way.

var s = document.myform.elements["myform"+i]; // this is a select element
alert(s.options[s.selectedIndex].value); // this is the selected value

Nik36
12-08-2006, 09:20 AM
Thanks Nikki, I will give this a try.:D

Got this:
var i=1
for (i=1;i<=4;i++)
{

var s = document.myform.elements["myorder"+i]; // this is a select element

if(s == s){
alert("WARNING DUPLICATE VALUE");
}else{
document.write("Success");
}
}

}

I think I have the conditional statement wrong as it does detect that the value of s = s but that would also include itself.

I'm trying to get it to detect that select1 is the same as select3 or that they all have different values so it's ok to submit.

nikkiH
12-11-2006, 04:40 PM
Check this out.



<html>
<script type="text/javascript">
function checkit(frm)
{
var MAXELEMENTS = 4;

for (var i=1; i<MAXELEMENTS; i++)
{
var value = frm.elements["myorder"+i].options[frm.elements["myorder"+i].selectedIndex].value;
for (var j=i+1; j<=MAXELEMENTS; j++)
{
if (value == frm.elements["myorder"+j].options[frm.elements["myorder"+j].selectedIndex].value)
{
alert("WARNING DUPLICATE VALUE");
return false;
}
else
{
alert("Success");
return true;
}
}
}
}
</script>
<body>
<p>test</p>
<form action="" method="get" onsubmit="return checkit(this)">
<p>
<select name="myorder1">
<option value="foo">foo</option>
<option value="bar">bar</option>
<option value="wow">wow</option>
<option value="bugs">bugs</option>
<option value="bunny">bunny</option>
</select>
</p>
<p> <select name="myorder2">
<option value="foo">foo</option>
<option value="bar">bar</option>
<option value="wow">wow</option>
<option value="bugs">bugs</option>
<option value="bunny">bunny</option>
</select>
</p>
<p> <select name="myorder3">
<option value="foo">foo</option>
<option value="bar">bar</option>
<option value="wow">wow</option>
<option value="bugs">bugs</option>
<option value="bunny">bunny</option>
</select>
</p>
<p>
<select name="myorder4">
<option value="foo">foo</option>
<option value="bar">bar</option>
<option value="wow">wow</option>
<option value="bugs">bugs</option>
<option value="bunny">bunny</option>
</select>
</p>
<p>
<input type="submit" name="Submit" value="Submit">
</p>
</form>
</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum