Javascript validation with no duplicates

I have three combo boxes with some entries in it. All of the three drop down contains the same values. The user has to select values from each drop down but cannot select the same value again. Can someone give me an example ?
Micheal_MaleAsked:
Who is Participating?
 
Micheal_MaleAuthor Commented:
I tried something like this but it does not seems to be working properly. Although the logic looks good :--).
if(document.formValidation.category.selectedIndex.value == document.formValidation.category2.selectedIndex.value){
	alert('category and category2 are the same.');
	return false;
	}
 
	if(document.formValidation.category2.selectedIndex.value == document.formValidation.category3.selectedIndex.value){
	alert('category2 and category 3 are the same.');
	return false;
	}
 
if(document.formValidation.category.selectedIndex.value == document.formValidation.category3.selectedIndex.value){
	alert('category and category3 are the same.');
	return false;
	}

Open in new window

0
 
Micheal_MaleAuthor Commented:
Here is my form
<html>
    <head>
    </head>
    <body>
    <form name="theForm" action="#" method="post" onsubmit="return validate()">
        
        Select category: <select name="category">
            <option value="0">0</option>
            <option value="9">9</option>
            <option value="12">12</option>
            <option value="23">23</option>
            <option value="cats">cats</option>
            <option value="dogs">dogs</option>
        </select>
        
        
        Select category: <select name="category2">
            <option value="0">0</option>
            <option value="9">9</option>
            <option value="12">12</option>
            <option value="23">23</option>
            <option value="cats">cats</option>
            <option value="dogs">dogs</option>
        </select>        
        
        Select category: <select name="category3">
            <option value="0">0</option>
            <option value="9">9</option>
            <option value="12">12</option>
            <option value="23">23</option>
            <option value="cats">cats</option>
            <option value="dogs">dogs</option>
        </select>             
        
        <input type="submit" name="submitButton" value="Submit">
    </form>
    </body>
</html>

Open in new window

0
 
ValleyENTCommented:
See Code
<SCRIPT LANGUAGE="JavaScript"'> 
<!--
function validateForm(){
if(document.ItemList.category1.selectedIndex==0)
{
alert("Please select an Item.");
document.ItemList.Item.focus();
return false;
}
return true;
}
//-->
</SCRIPT> 

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
ValleyENTCommented:
Typo
<SCRIPT LANGUAGE="JavaScript"'> 
<!--
function validateForm(){
if(document.ItemList.category1.selectedIndex==0)
{
alert("Please select an Item.");
document.ItemList.category1.focus();
return false;
}
return true;
}
//-->
</SCRIPT> 

Open in new window

0
 
Micheal_MaleAuthor Commented:
I believe the above code will validate only if the user will not select the values which i have already accomplished in the code snippet.  The problem is i want to compare the values selected for those 3 combo boxes. There should not be any duplicate in those 3 combo boxes selected by the user..

function isEmptyField(s)
{	var i;
	if (isEmpty(s)) return true;
	for (i = 0; i < s.length; i++)
	{
		var c = s.charAt(i);
		if (whitespace.indexOf(c) == -1) return false;
	}
	return true;
}

Open in new window

0
 
Micheal_MaleAuthor Commented:
And here is how i am calling that method.
function validateInput() {
 
if ( isEmptyField(document.formValidation.category.value) )
		{
		alert("No value is selected.");
		document.checkForm.state.focus();
		return false;
		}
return true;
}

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.