User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 429,793 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,818 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 6185 | Replies: 2
Reply
Join Date: Aug 2005
Posts: 26
Reputation: aish is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
aish's Avatar
aish aish is offline Offline
Light Poster

Compare two text feilds

  #1  
Jan 18th, 2006
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:
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jan 2006
Location: South Central PA
Posts: 13
Reputation: sfbell is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
sfbell's Avatar
sfbell sfbell is offline Offline
Newbie Poster

Re: Compare two text feilds

  #2  
Jan 18th, 2006
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[i].value == form[0].value && form[i] != form[0]){
test_# = false;
}else if(form[i].value == form[1].value && form[i] != form[1]){
test_# = false;
}

}
etc...


Originally Posted by aish
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:
Reply With Quote  
Join Date: Aug 2005
Posts: 26
Reputation: aish is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
aish's Avatar
aish aish is offline Offline
Light Poster

Solution Re: Compare two text feilds

  #3  
Jan 20th, 2006
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>
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb JavaScript / DHTML / AJAX Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 4:28 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC