We help IT Professionals succeed at work.

Javascript validation with no duplicates

Micheal_Male
Micheal_Male asked
on
Medium Priority
272 Views
Last Modified: 2012-05-06
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 ?
Comment
Watch Question

Author

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

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

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

Author

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

Author

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

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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.