Comparing multiple field values together
up vote
1
down vote
favorite
How can I compare multiple input field values and if there is a match alert 'There are similar values' using jQuery?
<input value="111"> //similar
<input value="222">
<input value="111"> //similar
<input value="333">
This html code above should alert 'There are similar values', as it has 2 values which are the same. How can it be done with jQuery?
My tried(Following code doesn't work):
DEMO: http://jsfiddle.net/HpWLQ/
$('input').each(function ()
var $this = $(this);
var val = $this.val();
vals.push(val);
);
for (var i = 0; i < vals.length; i++)
for (var n = 0; n < vals.length; n++)
if (n !== i)
if (vals[i] === vals[n])
alert('There are similar values');
javascript jquery
add a comment |
up vote
1
down vote
favorite
How can I compare multiple input field values and if there is a match alert 'There are similar values' using jQuery?
<input value="111"> //similar
<input value="222">
<input value="111"> //similar
<input value="333">
This html code above should alert 'There are similar values', as it has 2 values which are the same. How can it be done with jQuery?
My tried(Following code doesn't work):
DEMO: http://jsfiddle.net/HpWLQ/
$('input').each(function ()
var $this = $(this);
var val = $this.val();
vals.push(val);
);
for (var i = 0; i < vals.length; i++)
for (var n = 0; n < vals.length; n++)
if (n !== i)
if (vals[i] === vals[n])
alert('There are similar values');
javascript jquery
2
What have you try?
– Alexander
Apr 8 '12 at 12:18
Use developer.mozilla.org/en/JavaScript/Reference/Global_Objects/… or api.jquery.com/jQuery.inArray instead of nestedfor
loops.
– Stefan
Apr 8 '12 at 12:45
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
How can I compare multiple input field values and if there is a match alert 'There are similar values' using jQuery?
<input value="111"> //similar
<input value="222">
<input value="111"> //similar
<input value="333">
This html code above should alert 'There are similar values', as it has 2 values which are the same. How can it be done with jQuery?
My tried(Following code doesn't work):
DEMO: http://jsfiddle.net/HpWLQ/
$('input').each(function ()
var $this = $(this);
var val = $this.val();
vals.push(val);
);
for (var i = 0; i < vals.length; i++)
for (var n = 0; n < vals.length; n++)
if (n !== i)
if (vals[i] === vals[n])
alert('There are similar values');
javascript jquery
How can I compare multiple input field values and if there is a match alert 'There are similar values' using jQuery?
<input value="111"> //similar
<input value="222">
<input value="111"> //similar
<input value="333">
This html code above should alert 'There are similar values', as it has 2 values which are the same. How can it be done with jQuery?
My tried(Following code doesn't work):
DEMO: http://jsfiddle.net/HpWLQ/
$('input').each(function ()
var $this = $(this);
var val = $this.val();
vals.push(val);
);
for (var i = 0; i < vals.length; i++)
for (var n = 0; n < vals.length; n++)
if (n !== i)
if (vals[i] === vals[n])
alert('There are similar values');
javascript jquery
javascript jquery
edited Nov 10 at 11:56
Cœur
17.2k9102141
17.2k9102141
asked Apr 8 '12 at 12:14
jennifer Jolie
34951227
34951227
2
What have you try?
– Alexander
Apr 8 '12 at 12:18
Use developer.mozilla.org/en/JavaScript/Reference/Global_Objects/… or api.jquery.com/jQuery.inArray instead of nestedfor
loops.
– Stefan
Apr 8 '12 at 12:45
add a comment |
2
What have you try?
– Alexander
Apr 8 '12 at 12:18
Use developer.mozilla.org/en/JavaScript/Reference/Global_Objects/… or api.jquery.com/jQuery.inArray instead of nestedfor
loops.
– Stefan
Apr 8 '12 at 12:45
2
2
What have you try?
– Alexander
Apr 8 '12 at 12:18
What have you try?
– Alexander
Apr 8 '12 at 12:18
Use developer.mozilla.org/en/JavaScript/Reference/Global_Objects/… or api.jquery.com/jQuery.inArray instead of nested
for
loops.– Stefan
Apr 8 '12 at 12:45
Use developer.mozilla.org/en/JavaScript/Reference/Global_Objects/… or api.jquery.com/jQuery.inArray instead of nested
for
loops.– Stefan
Apr 8 '12 at 12:45
add a comment |
5 Answers
5
active
oldest
votes
up vote
2
down vote
accepted
Edit: In order to satisfy @Raynos, here's a pure JS solution.
var vals = ;
var flag = false;
var collection = document.getElementsByTagName('input');
collection = .slice.call(collection);
collection.forEach(function(element, index, array)
if (flag === true) return;
var i = element.value;
if (vals[i])
flag = true;
alert('There are duplicates!');
else
vals[i] = 1;
);
Bonus: I ran my solutions through jsperf; the last one is obviously by far the fastest.
Edit: Here's what would probably be the jQuery way of solving your problem (Working fiddle here). My original answer remains below.
var duplicatefound = false;
$('input').each(function(index, item)
if (duplicatefound) return;
val = $(item).val();
if ($('input[value="' + val + '"]').length == 1) return;
duplicatefound = true;
alert('There are similar values');
);
Alternative solution in case you need to capture some or all of the duplicate values (Working fiddle here):
var collection = $('input');
var duplicates = $.map(collection, function(item)
val = $(item).val();
return ($('input[value="' + val + '"]').length > 1) ? val : null;
);
if(duplicates.length > 0) alert('There are similar values');
As others have mentioned, you were missing a declaration of vals
as an array, therefore, .push()
was failing.
You might find an object based solution to be more elegant: Working fiddle
var vals = ;
$('input').each(function()
var index = $(this).val();
if (vals[index])
vals[index]++
else
vals[index] = 1;
);
var duplicates = $.map(vals, function(val, key)
return (val > 1) ? key : null;
);
if (duplicates.length > 0) alert('There are duplicates!');
The phrase "the fastest" in combination with jQuery is worth a downvote. The fastest solution is to always not use jQuery
– Raynos
Apr 8 '12 at 14:47
@Raynos I stated quite clearly that I jsperf'd my solutions. Of those, #3 was the fastest. Of course, not using jQuery at all would be faster.
– vzwick
Apr 8 '12 at 14:50
It's a tongue in cheek joke. The real downvote is for promoting code like$('input[value="' + val + '"]')
which is horrible
– Raynos
Apr 8 '12 at 14:52
@Raynos Oh, c'mon. Let's not get into pseudoreligious arguments about jQuery usage.
– vzwick
Apr 8 '12 at 14:56
1
The issue here is that we've not only created ugly code which is hard to read (or pass to someone else, like the people you want to answer a question), as well as maintain in a year, BUT we've also made a poor choice for how to locate things in the DOM. Unfortunately, we can't explain why this is terrible for working with the DOM in the comment boxes or on a Q&A site (I've tried, I nearly wrote a book in one answer, and it wasn't even complete). The best I can say is check out Mozilla Developer Network's guides on HTML/DOM/JS.
– Incognito
Apr 8 '12 at 15:06
|
show 3 more comments
up vote
1
down vote
You haven't defined the vals
to be an array. You can define it like
vals = ;
Demo
add a comment |
up vote
1
down vote
EDITED 2:
function findDuplicates()
var dupCount = 0;
$('input').each(function ()
var $currentInput = $(this);
$('input').each(function ()
if($(this).val() == $currentInput.val() && $(this) != $currentInput)
dupCount++;
);
);
if(dupCount > 0) alert("There are " + dupCount + "duplicates!");
1
Try running this on a set with just one duplicate, not to mention multiple ones ;) jsfiddle.net/pxp9n/5 There's plenty of alerts due to excessive iteration. You should set a flag if there was any duplicate andreturn
early in the firsteach
if that flag istrue
: jsfiddle.net/pxp9n/7
– vzwick
Apr 8 '12 at 13:14
Yes, you are right, that piece of code wasn't the best.
– Michael
Apr 8 '12 at 15:31
add a comment |
up vote
1
down vote
var inputs = $("input"), similar = , i, j;
for (i = 0; i < inputs.length; i += 1)
for (j = i + 1; j < inputs.length; j += 1)
if (inputs[i].value === inputs[j].value)
similar.push([i,j]);
if (similar.length > 0)
alert('There are similar values');
Note that the this only compares unique permutations in order, and it gives you an array of matching indices in the similar array, in case this is interesting to you.
Example: http://jsfiddle.net/sjCGy/
You could just as easily remove the jQuery dependency using document.getElementsByTagName instead of the jQuery selector.
And since @Raynos can't read, I'll make it explicit.
var inputs = document.getElementsByTagName("input"), similar = , i, j;
for (i = 0; i < inputs.length; i += 1)
for (j = i + 1; j < inputs.length; j += 1)
if (inputs[i].value === inputs[j].value)
similar.push([i,j]);
if (similar.length > 0)
alert('There are similar values');
-1 for not usingdocument.getElementsByTagName
– Raynos
Apr 8 '12 at 14:16
1
Did you actually read the answer?
– Matt Esch
Apr 8 '12 at 14:25
add a comment |
up vote
0
down vote
Your code works, you are just missing the declaration of the vals
array - See updated fiddle
I don't mean to be holier than the pope, but a) you're iterating over each input, redeclaringvals
to an empty array on every iteration b) you're checking for matches on every iteration, which is less than efficient.
– vzwick
Apr 8 '12 at 12:38
@vzwick Yeah, you're right. Don't know what I was thinking with that one.
– Rory McCrossan
Apr 8 '12 at 12:40
add a comment |
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Edit: In order to satisfy @Raynos, here's a pure JS solution.
var vals = ;
var flag = false;
var collection = document.getElementsByTagName('input');
collection = .slice.call(collection);
collection.forEach(function(element, index, array)
if (flag === true) return;
var i = element.value;
if (vals[i])
flag = true;
alert('There are duplicates!');
else
vals[i] = 1;
);
Bonus: I ran my solutions through jsperf; the last one is obviously by far the fastest.
Edit: Here's what would probably be the jQuery way of solving your problem (Working fiddle here). My original answer remains below.
var duplicatefound = false;
$('input').each(function(index, item)
if (duplicatefound) return;
val = $(item).val();
if ($('input[value="' + val + '"]').length == 1) return;
duplicatefound = true;
alert('There are similar values');
);
Alternative solution in case you need to capture some or all of the duplicate values (Working fiddle here):
var collection = $('input');
var duplicates = $.map(collection, function(item)
val = $(item).val();
return ($('input[value="' + val + '"]').length > 1) ? val : null;
);
if(duplicates.length > 0) alert('There are similar values');
As others have mentioned, you were missing a declaration of vals
as an array, therefore, .push()
was failing.
You might find an object based solution to be more elegant: Working fiddle
var vals = ;
$('input').each(function()
var index = $(this).val();
if (vals[index])
vals[index]++
else
vals[index] = 1;
);
var duplicates = $.map(vals, function(val, key)
return (val > 1) ? key : null;
);
if (duplicates.length > 0) alert('There are duplicates!');
The phrase "the fastest" in combination with jQuery is worth a downvote. The fastest solution is to always not use jQuery
– Raynos
Apr 8 '12 at 14:47
@Raynos I stated quite clearly that I jsperf'd my solutions. Of those, #3 was the fastest. Of course, not using jQuery at all would be faster.
– vzwick
Apr 8 '12 at 14:50
It's a tongue in cheek joke. The real downvote is for promoting code like$('input[value="' + val + '"]')
which is horrible
– Raynos
Apr 8 '12 at 14:52
@Raynos Oh, c'mon. Let's not get into pseudoreligious arguments about jQuery usage.
– vzwick
Apr 8 '12 at 14:56
1
The issue here is that we've not only created ugly code which is hard to read (or pass to someone else, like the people you want to answer a question), as well as maintain in a year, BUT we've also made a poor choice for how to locate things in the DOM. Unfortunately, we can't explain why this is terrible for working with the DOM in the comment boxes or on a Q&A site (I've tried, I nearly wrote a book in one answer, and it wasn't even complete). The best I can say is check out Mozilla Developer Network's guides on HTML/DOM/JS.
– Incognito
Apr 8 '12 at 15:06
|
show 3 more comments
up vote
2
down vote
accepted
Edit: In order to satisfy @Raynos, here's a pure JS solution.
var vals = ;
var flag = false;
var collection = document.getElementsByTagName('input');
collection = .slice.call(collection);
collection.forEach(function(element, index, array)
if (flag === true) return;
var i = element.value;
if (vals[i])
flag = true;
alert('There are duplicates!');
else
vals[i] = 1;
);
Bonus: I ran my solutions through jsperf; the last one is obviously by far the fastest.
Edit: Here's what would probably be the jQuery way of solving your problem (Working fiddle here). My original answer remains below.
var duplicatefound = false;
$('input').each(function(index, item)
if (duplicatefound) return;
val = $(item).val();
if ($('input[value="' + val + '"]').length == 1) return;
duplicatefound = true;
alert('There are similar values');
);
Alternative solution in case you need to capture some or all of the duplicate values (Working fiddle here):
var collection = $('input');
var duplicates = $.map(collection, function(item)
val = $(item).val();
return ($('input[value="' + val + '"]').length > 1) ? val : null;
);
if(duplicates.length > 0) alert('There are similar values');
As others have mentioned, you were missing a declaration of vals
as an array, therefore, .push()
was failing.
You might find an object based solution to be more elegant: Working fiddle
var vals = ;
$('input').each(function()
var index = $(this).val();
if (vals[index])
vals[index]++
else
vals[index] = 1;
);
var duplicates = $.map(vals, function(val, key)
return (val > 1) ? key : null;
);
if (duplicates.length > 0) alert('There are duplicates!');
The phrase "the fastest" in combination with jQuery is worth a downvote. The fastest solution is to always not use jQuery
– Raynos
Apr 8 '12 at 14:47
@Raynos I stated quite clearly that I jsperf'd my solutions. Of those, #3 was the fastest. Of course, not using jQuery at all would be faster.
– vzwick
Apr 8 '12 at 14:50
It's a tongue in cheek joke. The real downvote is for promoting code like$('input[value="' + val + '"]')
which is horrible
– Raynos
Apr 8 '12 at 14:52
@Raynos Oh, c'mon. Let's not get into pseudoreligious arguments about jQuery usage.
– vzwick
Apr 8 '12 at 14:56
1
The issue here is that we've not only created ugly code which is hard to read (or pass to someone else, like the people you want to answer a question), as well as maintain in a year, BUT we've also made a poor choice for how to locate things in the DOM. Unfortunately, we can't explain why this is terrible for working with the DOM in the comment boxes or on a Q&A site (I've tried, I nearly wrote a book in one answer, and it wasn't even complete). The best I can say is check out Mozilla Developer Network's guides on HTML/DOM/JS.
– Incognito
Apr 8 '12 at 15:06
|
show 3 more comments
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Edit: In order to satisfy @Raynos, here's a pure JS solution.
var vals = ;
var flag = false;
var collection = document.getElementsByTagName('input');
collection = .slice.call(collection);
collection.forEach(function(element, index, array)
if (flag === true) return;
var i = element.value;
if (vals[i])
flag = true;
alert('There are duplicates!');
else
vals[i] = 1;
);
Bonus: I ran my solutions through jsperf; the last one is obviously by far the fastest.
Edit: Here's what would probably be the jQuery way of solving your problem (Working fiddle here). My original answer remains below.
var duplicatefound = false;
$('input').each(function(index, item)
if (duplicatefound) return;
val = $(item).val();
if ($('input[value="' + val + '"]').length == 1) return;
duplicatefound = true;
alert('There are similar values');
);
Alternative solution in case you need to capture some or all of the duplicate values (Working fiddle here):
var collection = $('input');
var duplicates = $.map(collection, function(item)
val = $(item).val();
return ($('input[value="' + val + '"]').length > 1) ? val : null;
);
if(duplicates.length > 0) alert('There are similar values');
As others have mentioned, you were missing a declaration of vals
as an array, therefore, .push()
was failing.
You might find an object based solution to be more elegant: Working fiddle
var vals = ;
$('input').each(function()
var index = $(this).val();
if (vals[index])
vals[index]++
else
vals[index] = 1;
);
var duplicates = $.map(vals, function(val, key)
return (val > 1) ? key : null;
);
if (duplicates.length > 0) alert('There are duplicates!');
Edit: In order to satisfy @Raynos, here's a pure JS solution.
var vals = ;
var flag = false;
var collection = document.getElementsByTagName('input');
collection = .slice.call(collection);
collection.forEach(function(element, index, array)
if (flag === true) return;
var i = element.value;
if (vals[i])
flag = true;
alert('There are duplicates!');
else
vals[i] = 1;
);
Bonus: I ran my solutions through jsperf; the last one is obviously by far the fastest.
Edit: Here's what would probably be the jQuery way of solving your problem (Working fiddle here). My original answer remains below.
var duplicatefound = false;
$('input').each(function(index, item)
if (duplicatefound) return;
val = $(item).val();
if ($('input[value="' + val + '"]').length == 1) return;
duplicatefound = true;
alert('There are similar values');
);
Alternative solution in case you need to capture some or all of the duplicate values (Working fiddle here):
var collection = $('input');
var duplicates = $.map(collection, function(item)
val = $(item).val();
return ($('input[value="' + val + '"]').length > 1) ? val : null;
);
if(duplicates.length > 0) alert('There are similar values');
As others have mentioned, you were missing a declaration of vals
as an array, therefore, .push()
was failing.
You might find an object based solution to be more elegant: Working fiddle
var vals = ;
$('input').each(function()
var index = $(this).val();
if (vals[index])
vals[index]++
else
vals[index] = 1;
);
var duplicates = $.map(vals, function(val, key)
return (val > 1) ? key : null;
);
if (duplicates.length > 0) alert('There are duplicates!');
edited Apr 8 '12 at 15:26
answered Apr 8 '12 at 12:31
vzwick
8,25133358
8,25133358
The phrase "the fastest" in combination with jQuery is worth a downvote. The fastest solution is to always not use jQuery
– Raynos
Apr 8 '12 at 14:47
@Raynos I stated quite clearly that I jsperf'd my solutions. Of those, #3 was the fastest. Of course, not using jQuery at all would be faster.
– vzwick
Apr 8 '12 at 14:50
It's a tongue in cheek joke. The real downvote is for promoting code like$('input[value="' + val + '"]')
which is horrible
– Raynos
Apr 8 '12 at 14:52
@Raynos Oh, c'mon. Let's not get into pseudoreligious arguments about jQuery usage.
– vzwick
Apr 8 '12 at 14:56
1
The issue here is that we've not only created ugly code which is hard to read (or pass to someone else, like the people you want to answer a question), as well as maintain in a year, BUT we've also made a poor choice for how to locate things in the DOM. Unfortunately, we can't explain why this is terrible for working with the DOM in the comment boxes or on a Q&A site (I've tried, I nearly wrote a book in one answer, and it wasn't even complete). The best I can say is check out Mozilla Developer Network's guides on HTML/DOM/JS.
– Incognito
Apr 8 '12 at 15:06
|
show 3 more comments
The phrase "the fastest" in combination with jQuery is worth a downvote. The fastest solution is to always not use jQuery
– Raynos
Apr 8 '12 at 14:47
@Raynos I stated quite clearly that I jsperf'd my solutions. Of those, #3 was the fastest. Of course, not using jQuery at all would be faster.
– vzwick
Apr 8 '12 at 14:50
It's a tongue in cheek joke. The real downvote is for promoting code like$('input[value="' + val + '"]')
which is horrible
– Raynos
Apr 8 '12 at 14:52
@Raynos Oh, c'mon. Let's not get into pseudoreligious arguments about jQuery usage.
– vzwick
Apr 8 '12 at 14:56
1
The issue here is that we've not only created ugly code which is hard to read (or pass to someone else, like the people you want to answer a question), as well as maintain in a year, BUT we've also made a poor choice for how to locate things in the DOM. Unfortunately, we can't explain why this is terrible for working with the DOM in the comment boxes or on a Q&A site (I've tried, I nearly wrote a book in one answer, and it wasn't even complete). The best I can say is check out Mozilla Developer Network's guides on HTML/DOM/JS.
– Incognito
Apr 8 '12 at 15:06
The phrase "the fastest" in combination with jQuery is worth a downvote. The fastest solution is to always not use jQuery
– Raynos
Apr 8 '12 at 14:47
The phrase "the fastest" in combination with jQuery is worth a downvote. The fastest solution is to always not use jQuery
– Raynos
Apr 8 '12 at 14:47
@Raynos I stated quite clearly that I jsperf'd my solutions. Of those, #3 was the fastest. Of course, not using jQuery at all would be faster.
– vzwick
Apr 8 '12 at 14:50
@Raynos I stated quite clearly that I jsperf'd my solutions. Of those, #3 was the fastest. Of course, not using jQuery at all would be faster.
– vzwick
Apr 8 '12 at 14:50
It's a tongue in cheek joke. The real downvote is for promoting code like
$('input[value="' + val + '"]')
which is horrible– Raynos
Apr 8 '12 at 14:52
It's a tongue in cheek joke. The real downvote is for promoting code like
$('input[value="' + val + '"]')
which is horrible– Raynos
Apr 8 '12 at 14:52
@Raynos Oh, c'mon. Let's not get into pseudoreligious arguments about jQuery usage.
– vzwick
Apr 8 '12 at 14:56
@Raynos Oh, c'mon. Let's not get into pseudoreligious arguments about jQuery usage.
– vzwick
Apr 8 '12 at 14:56
1
1
The issue here is that we've not only created ugly code which is hard to read (or pass to someone else, like the people you want to answer a question), as well as maintain in a year, BUT we've also made a poor choice for how to locate things in the DOM. Unfortunately, we can't explain why this is terrible for working with the DOM in the comment boxes or on a Q&A site (I've tried, I nearly wrote a book in one answer, and it wasn't even complete). The best I can say is check out Mozilla Developer Network's guides on HTML/DOM/JS.
– Incognito
Apr 8 '12 at 15:06
The issue here is that we've not only created ugly code which is hard to read (or pass to someone else, like the people you want to answer a question), as well as maintain in a year, BUT we've also made a poor choice for how to locate things in the DOM. Unfortunately, we can't explain why this is terrible for working with the DOM in the comment boxes or on a Q&A site (I've tried, I nearly wrote a book in one answer, and it wasn't even complete). The best I can say is check out Mozilla Developer Network's guides on HTML/DOM/JS.
– Incognito
Apr 8 '12 at 15:06
|
show 3 more comments
up vote
1
down vote
You haven't defined the vals
to be an array. You can define it like
vals = ;
Demo
add a comment |
up vote
1
down vote
You haven't defined the vals
to be an array. You can define it like
vals = ;
Demo
add a comment |
up vote
1
down vote
up vote
1
down vote
You haven't defined the vals
to be an array. You can define it like
vals = ;
Demo
You haven't defined the vals
to be an array. You can define it like
vals = ;
Demo
answered Apr 8 '12 at 12:23
Starx
58.2k35155239
58.2k35155239
add a comment |
add a comment |
up vote
1
down vote
EDITED 2:
function findDuplicates()
var dupCount = 0;
$('input').each(function ()
var $currentInput = $(this);
$('input').each(function ()
if($(this).val() == $currentInput.val() && $(this) != $currentInput)
dupCount++;
);
);
if(dupCount > 0) alert("There are " + dupCount + "duplicates!");
1
Try running this on a set with just one duplicate, not to mention multiple ones ;) jsfiddle.net/pxp9n/5 There's plenty of alerts due to excessive iteration. You should set a flag if there was any duplicate andreturn
early in the firsteach
if that flag istrue
: jsfiddle.net/pxp9n/7
– vzwick
Apr 8 '12 at 13:14
Yes, you are right, that piece of code wasn't the best.
– Michael
Apr 8 '12 at 15:31
add a comment |
up vote
1
down vote
EDITED 2:
function findDuplicates()
var dupCount = 0;
$('input').each(function ()
var $currentInput = $(this);
$('input').each(function ()
if($(this).val() == $currentInput.val() && $(this) != $currentInput)
dupCount++;
);
);
if(dupCount > 0) alert("There are " + dupCount + "duplicates!");
1
Try running this on a set with just one duplicate, not to mention multiple ones ;) jsfiddle.net/pxp9n/5 There's plenty of alerts due to excessive iteration. You should set a flag if there was any duplicate andreturn
early in the firsteach
if that flag istrue
: jsfiddle.net/pxp9n/7
– vzwick
Apr 8 '12 at 13:14
Yes, you are right, that piece of code wasn't the best.
– Michael
Apr 8 '12 at 15:31
add a comment |
up vote
1
down vote
up vote
1
down vote
EDITED 2:
function findDuplicates()
var dupCount = 0;
$('input').each(function ()
var $currentInput = $(this);
$('input').each(function ()
if($(this).val() == $currentInput.val() && $(this) != $currentInput)
dupCount++;
);
);
if(dupCount > 0) alert("There are " + dupCount + "duplicates!");
EDITED 2:
function findDuplicates()
var dupCount = 0;
$('input').each(function ()
var $currentInput = $(this);
$('input').each(function ()
if($(this).val() == $currentInput.val() && $(this) != $currentInput)
dupCount++;
);
);
if(dupCount > 0) alert("There are " + dupCount + "duplicates!");
edited Apr 8 '12 at 15:28
answered Apr 8 '12 at 12:25
Michael
2,12883963
2,12883963
1
Try running this on a set with just one duplicate, not to mention multiple ones ;) jsfiddle.net/pxp9n/5 There's plenty of alerts due to excessive iteration. You should set a flag if there was any duplicate andreturn
early in the firsteach
if that flag istrue
: jsfiddle.net/pxp9n/7
– vzwick
Apr 8 '12 at 13:14
Yes, you are right, that piece of code wasn't the best.
– Michael
Apr 8 '12 at 15:31
add a comment |
1
Try running this on a set with just one duplicate, not to mention multiple ones ;) jsfiddle.net/pxp9n/5 There's plenty of alerts due to excessive iteration. You should set a flag if there was any duplicate andreturn
early in the firsteach
if that flag istrue
: jsfiddle.net/pxp9n/7
– vzwick
Apr 8 '12 at 13:14
Yes, you are right, that piece of code wasn't the best.
– Michael
Apr 8 '12 at 15:31
1
1
Try running this on a set with just one duplicate, not to mention multiple ones ;) jsfiddle.net/pxp9n/5 There's plenty of alerts due to excessive iteration. You should set a flag if there was any duplicate and
return
early in the first each
if that flag is true
: jsfiddle.net/pxp9n/7– vzwick
Apr 8 '12 at 13:14
Try running this on a set with just one duplicate, not to mention multiple ones ;) jsfiddle.net/pxp9n/5 There's plenty of alerts due to excessive iteration. You should set a flag if there was any duplicate and
return
early in the first each
if that flag is true
: jsfiddle.net/pxp9n/7– vzwick
Apr 8 '12 at 13:14
Yes, you are right, that piece of code wasn't the best.
– Michael
Apr 8 '12 at 15:31
Yes, you are right, that piece of code wasn't the best.
– Michael
Apr 8 '12 at 15:31
add a comment |
up vote
1
down vote
var inputs = $("input"), similar = , i, j;
for (i = 0; i < inputs.length; i += 1)
for (j = i + 1; j < inputs.length; j += 1)
if (inputs[i].value === inputs[j].value)
similar.push([i,j]);
if (similar.length > 0)
alert('There are similar values');
Note that the this only compares unique permutations in order, and it gives you an array of matching indices in the similar array, in case this is interesting to you.
Example: http://jsfiddle.net/sjCGy/
You could just as easily remove the jQuery dependency using document.getElementsByTagName instead of the jQuery selector.
And since @Raynos can't read, I'll make it explicit.
var inputs = document.getElementsByTagName("input"), similar = , i, j;
for (i = 0; i < inputs.length; i += 1)
for (j = i + 1; j < inputs.length; j += 1)
if (inputs[i].value === inputs[j].value)
similar.push([i,j]);
if (similar.length > 0)
alert('There are similar values');
-1 for not usingdocument.getElementsByTagName
– Raynos
Apr 8 '12 at 14:16
1
Did you actually read the answer?
– Matt Esch
Apr 8 '12 at 14:25
add a comment |
up vote
1
down vote
var inputs = $("input"), similar = , i, j;
for (i = 0; i < inputs.length; i += 1)
for (j = i + 1; j < inputs.length; j += 1)
if (inputs[i].value === inputs[j].value)
similar.push([i,j]);
if (similar.length > 0)
alert('There are similar values');
Note that the this only compares unique permutations in order, and it gives you an array of matching indices in the similar array, in case this is interesting to you.
Example: http://jsfiddle.net/sjCGy/
You could just as easily remove the jQuery dependency using document.getElementsByTagName instead of the jQuery selector.
And since @Raynos can't read, I'll make it explicit.
var inputs = document.getElementsByTagName("input"), similar = , i, j;
for (i = 0; i < inputs.length; i += 1)
for (j = i + 1; j < inputs.length; j += 1)
if (inputs[i].value === inputs[j].value)
similar.push([i,j]);
if (similar.length > 0)
alert('There are similar values');
-1 for not usingdocument.getElementsByTagName
– Raynos
Apr 8 '12 at 14:16
1
Did you actually read the answer?
– Matt Esch
Apr 8 '12 at 14:25
add a comment |
up vote
1
down vote
up vote
1
down vote
var inputs = $("input"), similar = , i, j;
for (i = 0; i < inputs.length; i += 1)
for (j = i + 1; j < inputs.length; j += 1)
if (inputs[i].value === inputs[j].value)
similar.push([i,j]);
if (similar.length > 0)
alert('There are similar values');
Note that the this only compares unique permutations in order, and it gives you an array of matching indices in the similar array, in case this is interesting to you.
Example: http://jsfiddle.net/sjCGy/
You could just as easily remove the jQuery dependency using document.getElementsByTagName instead of the jQuery selector.
And since @Raynos can't read, I'll make it explicit.
var inputs = document.getElementsByTagName("input"), similar = , i, j;
for (i = 0; i < inputs.length; i += 1)
for (j = i + 1; j < inputs.length; j += 1)
if (inputs[i].value === inputs[j].value)
similar.push([i,j]);
if (similar.length > 0)
alert('There are similar values');
var inputs = $("input"), similar = , i, j;
for (i = 0; i < inputs.length; i += 1)
for (j = i + 1; j < inputs.length; j += 1)
if (inputs[i].value === inputs[j].value)
similar.push([i,j]);
if (similar.length > 0)
alert('There are similar values');
Note that the this only compares unique permutations in order, and it gives you an array of matching indices in the similar array, in case this is interesting to you.
Example: http://jsfiddle.net/sjCGy/
You could just as easily remove the jQuery dependency using document.getElementsByTagName instead of the jQuery selector.
And since @Raynos can't read, I'll make it explicit.
var inputs = document.getElementsByTagName("input"), similar = , i, j;
for (i = 0; i < inputs.length; i += 1)
for (j = i + 1; j < inputs.length; j += 1)
if (inputs[i].value === inputs[j].value)
similar.push([i,j]);
if (similar.length > 0)
alert('There are similar values');
edited Apr 8 '12 at 21:41
answered Apr 8 '12 at 12:31
Matt Esch
18.4k64248
18.4k64248
-1 for not usingdocument.getElementsByTagName
– Raynos
Apr 8 '12 at 14:16
1
Did you actually read the answer?
– Matt Esch
Apr 8 '12 at 14:25
add a comment |
-1 for not usingdocument.getElementsByTagName
– Raynos
Apr 8 '12 at 14:16
1
Did you actually read the answer?
– Matt Esch
Apr 8 '12 at 14:25
-1 for not using
document.getElementsByTagName
– Raynos
Apr 8 '12 at 14:16
-1 for not using
document.getElementsByTagName
– Raynos
Apr 8 '12 at 14:16
1
1
Did you actually read the answer?
– Matt Esch
Apr 8 '12 at 14:25
Did you actually read the answer?
– Matt Esch
Apr 8 '12 at 14:25
add a comment |
up vote
0
down vote
Your code works, you are just missing the declaration of the vals
array - See updated fiddle
I don't mean to be holier than the pope, but a) you're iterating over each input, redeclaringvals
to an empty array on every iteration b) you're checking for matches on every iteration, which is less than efficient.
– vzwick
Apr 8 '12 at 12:38
@vzwick Yeah, you're right. Don't know what I was thinking with that one.
– Rory McCrossan
Apr 8 '12 at 12:40
add a comment |
up vote
0
down vote
Your code works, you are just missing the declaration of the vals
array - See updated fiddle
I don't mean to be holier than the pope, but a) you're iterating over each input, redeclaringvals
to an empty array on every iteration b) you're checking for matches on every iteration, which is less than efficient.
– vzwick
Apr 8 '12 at 12:38
@vzwick Yeah, you're right. Don't know what I was thinking with that one.
– Rory McCrossan
Apr 8 '12 at 12:40
add a comment |
up vote
0
down vote
up vote
0
down vote
Your code works, you are just missing the declaration of the vals
array - See updated fiddle
Your code works, you are just missing the declaration of the vals
array - See updated fiddle
edited Apr 8 '12 at 12:39
answered Apr 8 '12 at 12:26
Rory McCrossan
240k29203244
240k29203244
I don't mean to be holier than the pope, but a) you're iterating over each input, redeclaringvals
to an empty array on every iteration b) you're checking for matches on every iteration, which is less than efficient.
– vzwick
Apr 8 '12 at 12:38
@vzwick Yeah, you're right. Don't know what I was thinking with that one.
– Rory McCrossan
Apr 8 '12 at 12:40
add a comment |
I don't mean to be holier than the pope, but a) you're iterating over each input, redeclaringvals
to an empty array on every iteration b) you're checking for matches on every iteration, which is less than efficient.
– vzwick
Apr 8 '12 at 12:38
@vzwick Yeah, you're right. Don't know what I was thinking with that one.
– Rory McCrossan
Apr 8 '12 at 12:40
I don't mean to be holier than the pope, but a) you're iterating over each input, redeclaring
vals
to an empty array on every iteration b) you're checking for matches on every iteration, which is less than efficient.– vzwick
Apr 8 '12 at 12:38
I don't mean to be holier than the pope, but a) you're iterating over each input, redeclaring
vals
to an empty array on every iteration b) you're checking for matches on every iteration, which is less than efficient.– vzwick
Apr 8 '12 at 12:38
@vzwick Yeah, you're right. Don't know what I was thinking with that one.
– Rory McCrossan
Apr 8 '12 at 12:40
@vzwick Yeah, you're right. Don't know what I was thinking with that one.
– Rory McCrossan
Apr 8 '12 at 12:40
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f10062708%2fcomparing-multiple-field-values-together%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
What have you try?
– Alexander
Apr 8 '12 at 12:18
Use developer.mozilla.org/en/JavaScript/Reference/Global_Objects/… or api.jquery.com/jQuery.inArray instead of nested
for
loops.– Stefan
Apr 8 '12 at 12:45