In my asp.net c# web app. I have 10 text feilds. I want to avoid duplicate same value in this text feilds. how I can do this using javascripts. :cry:

Comparing two text fields is easy enough:
var textfield1 = form[0].field1.value;
// better yet use document.getElementByID('field1')
var textfield2 = form[0].field2.value;
// better yet use document.getElementByID('field2')
// then
if(textfield1 == textfield2){
// Do something when they are equal.
}else{
// Do something else when not equal.
}

The problem is trying to validate all ten fields.
A long solution would be to create an array of ten variables, to hold test results.

then take the ten fields, put them in an array.
The loop over the array performing the checks...

for(int i = 0; i < theArray.Length; i++){
// test to see if the values are the same, but make sure you
// aren't comparing the form field with itself.
if(form.value == form[0].value && form != form[0]){
test_# = false;
}else if(form.value == form[1].value && form != form[1]){
test_# = false;
}

}
etc...


In my asp.net c# web app. I have 10 text feilds. I want to avoid duplicate same value in this text feilds. how I can do this using javascripts. :cry:

Thanks for you reply :eek: . I found more efficient code.

<script type="text/javascript">
function Validate(objForm) {
	var arrNames=new Array("text1", "text2", "text3", "text4", "text5");
	var arrValues=new Array();
	for (var i=0; i<arrNames.length; i++) {
		var curValue = objForm.elements[arrNames[i]].value;
		if (arrValues[curValue]) {
			alert("can't have duplicate!");
			return false;
		}
		arrValues[curValue] = arrNames[i];
	}
	return true;
}
</script>
<form onsubmit="return Validate(this);">
<input type="text" name="text1" /><input type="text" name="text2" /><input type="text" name="text3" />
<input type="text" name="text4" /><input type="text" name="text5" /><button type="submit">Submit</button>
</form>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.