?
Solved

Javascript validation with no duplicates

Posted on 2009-02-17
6
Medium Priority
?
263 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 ?
0
Comment
Question by:Micheal_Male
  • 4
  • 2
6 Comments
 

Author Comment

by:Micheal_Male
ID: 23667137
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
 
LVL 4

Expert Comment

by:ValleyENT
ID: 23667179
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
 
LVL 4

Expert Comment

by:ValleyENT
ID: 23667185
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:Micheal_Male
ID: 23667207
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
 

Author Comment

by:Micheal_Male
ID: 23667220
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
 

Accepted Solution

by:
Micheal_Male earned 0 total points
ID: 23667229
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question