PDA

View Full Version : What's best way to check for duplicates in input fields?

PeaTearGriffin
08-18-2008, 04:20 PM
Let's say I have 20 rows of input fields and the user is supposed to input 20 (or can leave some blank) answers. I want to check if there are any duplicate answers. What would be the best way to do this? My way would be dumb and I'm sure there is a better way than doing "if field 1 == field 2 || field 3 || field 4..."

oesxyl
08-18-2008, 04:32 PM
Let's say I have 20 rows of input fields and the user is supposed to input 20 (or can leave some blank) answers. I want to check if there are any duplicate answers. What would be the best way to do this? My way would be dumb and I'm sure there is a better way than doing "if field 1 == field 2 || field 3 || field 4..."

function b_duplicate(arr){
for(var i = 0; i < arr.length; i++){
if(arr.indexOf(arr[i], i+1) > -1){
return true;
}
}
return false;
}

not tested. arr is a array with the values from input.

regards

Philip M
08-18-2008, 04:33 PM
Try this:-

<script type = "text/javascript">

var X = new Array( // just some sample data
"Bill",
"Jack",
"James",
"",
"Philip",
"Barry",
"Harry",
"",
"George",
"Freddie",
"Eric",
"Donald",
"Charles",
"Barry" // there's the duplicate
);

function arrHasDupes( A ) { // finds any duplicate array elements using the fewest possible comparison
var i, j, n;
n = A.length;
// to ensure the fewest possible comparisons
for (i = 0; i < n; i++) { // outer loop uses each item i at 0 through n
for (j = i+1; j < n; j++) { // inner loop only compares items j at i+1 to n
if (A[i] != "") { // ignore blanks
if (A[i].toLowerCase() == A[j].toLowerCase()) {return true} // case insensitive
}
}
}
return false;
}

if ( arrHasDupes( X )) { // this just calls the function to test it
document.write( "<H1>Yes</H1>");
}
else {
document.write( "<H1>No</H1>");
}

</script>

Take my advice, I don't use it anyway.

PeaTearGriffin
08-18-2008, 05:40 PM
Thanks for the help guys. I am getting worse and worse at JS :(
Not sure what I'm doing, but I tried to implement it with this:

<script type = "text/javascript">
function arrHasDupes( A ) { // finds any duplicate array elements using the fewest possible comparison
var i, j, n;
n = A.length;
// to ensure the fewest possible comparisons
for (i = 0; i < n; i++) { // outer loop uses each item i at 0 through n
for (j = i+1; j < n; j++) { // inner loop only compares items j at i+1 to n
if (A[i] != "") { // ignore blanks
if (A[i].toLowerCase() == A[j].toLowerCase()) {return true} // case insensitive
}
}
}
return false;
}

function checkdupes(){
debugger;
var X = new Array;
for (i=1; i<=5; i++)
{
X[] = document.getElementById("a" + i).value;
}

if ( arrHasDupes( X )){ // this just calls the function to test it
}
else {
}
}

</script>
<form action="" method="post">
<input type="text" id="a1" name="embed_link[]" /><br />
<input type="text" id="a2" name="embed_link[]" /><br />
<input type="text" id="a3" name="embed_link[]" /><br />
<input type="text" id="a4" name="embed_link[]" /><br />
<input type="text" id="a5" name="embed_link[]" /><br />

<input type="submit" name="submit" value="submit" onclick="checkdupes();return false" />
</form>

Doesn't seem to work for me. How do I grab the array of values from the input fields? I even forgot how to make firebug show up lol.

Philip M
08-18-2008, 05:49 PM
Sorry, I do not see what is wrong with what I showed you. It works fine for me.

var X = new Array ();
for (var i=1; i<=5; i++)
{
X[i] = document.getElementById("a[i]").value;
}

PeaTearGriffin
08-18-2008, 05:57 PM
Sorry, I do not see what is wrong with what I showed you. It works fine for me.

var X = new Array ();
for (var i=1; i<=5; i++)
{
X[i] = document.getElementById("a[i]").value;
}
No, no, nothing is wrong with your code. It's just after I tried to use it, I messed up something lol :p

Do I even need the name attribute in my input fields at all? I've been working more on practicing php, which is how I grab the array from inputs that way.

I made the changes, like this:

<script type = "text/javascript">
function arrHasDupes( A ) { // finds any duplicate array elements using the fewest possible comparison
var i, j, n;
n = A.length;
// to ensure the fewest possible comparisons
for (i = 0; i < n; i++) { // outer loop uses each item i at 0 through n
for (j = i+1; j < n; j++) { // inner loop only compares items j at i+1 to n
if (A[i] != "") { // ignore blanks
if (A[i].toLowerCase() == A[j].toLowerCase()) {return true} // case insensitive
}
}
}
return false;
}

function checkdupes(){
debugger;
var X = new Array();
for (i=1; i<=5; i++)
{
X[i] = document.getElementById("a[i]").value;
}

if ( arrHasDupes( X )){ // this just calls the function to test it
}
else {
}
}

</script>
<form action="" method="post">
<input type="text" id="a1" /><br />
<input type="text" id="a2" /><br />
<input type="text" id="a3" /><br />
<input type="text" id="a4" /><br />
<input type="text" id="a5" /><br />

<input type="submit" name="submit" value="submit" onclick="checkdupes();return false" />
</form>

If you just copy and paste that and try to run it, no matter what you input it just reloads the page. See anything wrong?

Philip M
08-18-2008, 06:10 PM
1<input type = "text" name = "txt1" id = "txt1"><br>
2<input type = "text" name = "txt2" id = "txt2"><br>
3<input type = "text" name = "txt3" id = "txt3"><br>
4<input type = "text" name = "txt4" id = "txt4"><br>
5<input type = "text" name = "txt5" id = "txt5"><br>

<input type = "button" value ="Check Entries" onclick = "check4dups()">

<script type = "text/javascript">
var X = new Array();
function check4dups() {
for (var i = 1; i<=5; i++) {
X[i] = document.getElementById("txt" + i).value;
}
if ( arrHasDupes( X )){ // this just calls the function to test it
alert("Yes, the array contains one or more duplicates ");
}
else {
alert("No, there are no duplicates in the array ");
}
}
</script>

Cranford
08-18-2008, 06:30 PM
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<title>Any Title</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">

function validate(nForm){

var allValues = [];
for (i=0; i<nFields.length; i++)
{
if (nFields[i].value != "")
{
allValues[i] = nFields[i].value.toLowerCase();
}
}
allValues.sort();
var uniqueValues = [];
var idx = 0;
var prevValue = allValues[0];
var currValue = allValues[0];
for (i=0; i<allValues.length; i++)
{
currValue = allValues[i];
if (currValue != prevValue){idx++;}
uniqueValues[idx] = currValue;
prevValue = currValue;
}
if (uniqueValues.length != allValues.length)
{
return false;
}
else {
return true;
}
}

</script>
<style type="text/css">

body {background-color:#eae3c6;margin-top:60px}
form {width:620px;margin:auto;font-family:times;font-size:12pt}
fieldset {width:610px;background-color:#f0fff0;border:1px solid #87ceeb}
.submitBtn {font-family:tahoma;font-size:10pt;display:block;margin-left:auto;margin-right:auto;margin-top:5px;margin-bottom:5px}

</style>
<body>
<form action="" method="post" onsubmit="return validate(this)">
<fieldset>
<legend>Form</legend>