Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form Validation - unique data 1

Status
Not open for further replies.

solfer

Programmer
Jan 17, 2002
7
0
0
GB
Hi each,

I'm looking for way to validate a form client-side, for the following:

The form has 5 <select> elements - each of them populated with the same data.

I want the validation to check that the selected value for all 5 elements is unique and not duplicated.

For various reasons, I don't want to use a multiple select to get around this...

Can any give me a few clues what JS I need to do this?

TIA Sol
 
if all 5 have the same options, just check the index:
Code:
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;>
<html>
	<head>
		<title>test</title>
		<script type=&quot;text/javascript&quot;>
		function isUnique() {
			var f = document.forms[0];
			var arSel = [&quot;select1&quot;,&quot;select2&quot;,&quot;select3&quot;,&quot;select4&quot;,&quot;select5&quot;];
			var error = false;

			outer:
			for (var x = 0; x < arSel.length; x++) {
				var index = f[arSel[x]].selectedIndex;

				for (var y = 0; y < arSel.length; y++) {
					if (x != y) 
						if (f[arSel[y]].selectedIndex == index) {
							error = true;
							break outer;
						}
				}
			}
			return !error;
		}
		</script>
	</head>

	<body>
		<form>
			<select name=&quot;select1&quot;>
				<option value=&quot;1&quot;>1</option>
				<option value=&quot;2&quot;>2</option>
				<option value=&quot;3&quot;>3</option>
				<option value=&quot;4&quot;>4</option>
				<option value=&quot;5&quot;>5</option>
			</select>
			<select name=&quot;select2&quot;>
				<option value=&quot;1&quot;>1</option>
				<option value=&quot;2&quot;>2</option>
				<option value=&quot;3&quot;>3</option>
				<option value=&quot;4&quot;>4</option>
				<option value=&quot;5&quot;>5</option>
			</select>
			<select name=&quot;select3&quot;>
				<option value=&quot;1&quot;>1</option>
				<option value=&quot;2&quot;>2</option>
				<option value=&quot;3&quot;>3</option>
				<option value=&quot;4&quot;>4</option>
				<option value=&quot;5&quot;>5</option>
			</select>
			<select name=&quot;select4&quot;>
				<option value=&quot;1&quot;>1</option>
				<option value=&quot;2&quot;>2</option>
				<option value=&quot;3&quot;>3</option>
				<option value=&quot;4&quot;>4</option>
				<option value=&quot;5&quot;>5</option>
			</select>
			<select name=&quot;select5&quot;>
				<option value=&quot;1&quot;>1</option>
				<option value=&quot;2&quot;>2</option>
				<option value=&quot;3&quot;>3</option>
				<option value=&quot;4&quot;>4</option>
				<option value=&quot;5&quot;>5</option>
			</select>
			<input type=&quot;button&quot; value=&quot;isUnique&quot; onclick=&quot;alert(isUnique())&quot; />
		</form>
	</body>
</html>

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
You absolute Star!! Thanks a million jeff :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top