...

View Full Version : array validation



newbie2006
06-28-2006, 08:46 AM
hi i am very new to javascript and will appreciate some help

i have an array of drop down list whose values i get from the sql database. I need to check whether the students selected for any of the drop down list is the same. If so , it should return an error msg.

is there anyway to do this in javascript?

Kor
06-28-2006, 10:28 AM
Welcome to the forum!


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
function verify(s){
var alls = document.getElementsByTagName('select');
for(var i=0;i<alls.length;i++){
s!=alls[i]&&alls[i].value==s.value?alert('Common option selected'):null
}
}
</script>
</head>
<body>
<select onchange="verify(this)">
<option value="john">john</option>
<option value="ann">ann</option>
<option value="george">george</option>
</select>
<select onchange="verify(this)">
<option value="ann">ann</option>
<option value="george">george</option>
<option value="john">john</option>
</select>
<select onchange="verify(this)">
<option value="george">george</option>
<option value="john">john</option>
<option value="ann">ann</option>
</select>
</body>
</html>

Next time try to show us that you have tried alone to solve the problem somehow, present a code, even wrong written, because you can not expect always the others will do all your work...

newbie2006
07-03-2006, 04:57 AM
i have another question. Building on what you mentioned in your previous post, if supposingly i have a default selected option i.e.

<select onchange="verify(this)">
<option selected value="default">--choose 1 option--</option>
<option value="john">john</option>
<option value="ann">ann</option>
<option value="george">george</option>
</select>

i dont want the validation to work if the option choosen is this.

i tried to do this

<script type="text/javascript">
function verify(s){
var alls = document.getElementsByTagName('select');
if (s.value!="default"){
for(var i=0;i<alls.length;i++){
s!=alls[i]&&alls[i].value==s.value?alert('Common option selected'):null
}
}
}
</script>
and it's not working - the prompt keep coming up, even though i do not want it to work if the option chosen is default

Kor
07-13-2006, 11:09 PM
try

if(s.selectedIndex>0){
...

Sorry for delay, I was on my vacation :-)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum