•
•
•
•
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 397,876 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 2,576 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: 5681 | Replies: 2
![]() |
•
•
Join Date: Jan 2006
Location: South Central PA
Posts: 13
Reputation:
Rep Power: 3
Solved Threads: 0
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...
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:
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>
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb JavaScript / DHTML / AJAX Marketplace
- Compare two Text File (Perl)
- string: get token from string and compare token from text file (C++)
- Electronic Address Book: C++ (C++)
- How can I get Highest, Lowest and Average ? (C++)
- Text File Input and manipulation (C++)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: javascript-firefox issues
- Next Thread: write issues


Linear Mode