Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Apr 2006
    Posts
    186
    Thanks
    21
    Thanked 6 Times in 6 Posts

    What's best way to check for duplicates in input fields?

    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..."

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by PeaTearGriffin View Post
    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..."
    Code:
    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

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,907
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Try this:-

    Code:
    <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.
    Last edited by Philip M; 08-18-2008 at 03:52 PM. Reason: Added ignore blanks

  • #4
    Regular Coder
    Join Date
    Apr 2006
    Posts
    186
    Thanks
    21
    Thanked 6 Times in 6 Posts
    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:

    Code:
    <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
    		alert("Yes Has Dupes");
    	}
    	else {
    		alert("No Dupes");
    	}
    }
    
    </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.

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,907
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Sorry, I do not see what is wrong with what I showed you. It works fine for me.


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

  • #6
    Regular Coder
    Join Date
    Apr 2006
    Posts
    186
    Thanks
    21
    Thanked 6 Times in 6 Posts
    Quote Originally Posted by Philip M View Post
    Sorry, I do not see what is wrong with what I showed you. It works fine for me.


    Code:
    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

    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:
    Code:
    <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
    		alert("Yes Has Dupes");
    	}
    	else {
    		alert("No Dupes");
    	}
    }
    
    </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?

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,907
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Code:
    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;
    }
    alert (X);  //delete after testing
    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>
    Last edited by Philip M; 08-18-2008 at 05:15 PM.

  • Users who have thanked Philip M for this post:

    PeaTearGriffin (08-18-2008)

  • #8
    Banned
    Join Date
    May 2005
    Location
    Midwest, U.S.
    Posts
    118
    Thanks
    1
    Thanked 26 Times in 23 Posts
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Any Title</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript">
    
    	function validate(nForm){
    
    		var nFields = document.getElementsByName('embed_link[]');
    		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)
    			{
    			 alert('Cannot submit duplicate entries');
    			 return false;
    			}
    		else 	{
    			 alert('Thank you for your submission');
    			 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}
    	 legend {font-family:times;font-size:14pt;color:#00008b;background-color:#87ceeb;padding-left:3px;padding-right:3px;margin-bottom:5px}
    	.submitBtn {font-family:tahoma;font-size:10pt;display:block;margin-left:auto;margin-right:auto;margin-top:5px;margin-bottom:5px}
    
    </style>
    </head>
    	<body>
    		<form action="" method="post" onsubmit="return validate(this)">
    		   <fieldset>
    			<legend>Form</legend>
    				<input type="text" name="embed_link[]" value="hello"><br>
    				<input type="text" name="embed_link[]" value="night"><br>
    				<input type="text" name="embed_link[]" value="HELLO"><br>
    				<input type="text" name="embed_link[]" value=""><br>
    				<input type="text" name="embed_link[]" value=""><br>
    				<input type="submit" name="submit" value="Submit" class="submitBtn">
    		   </fieldset>
    		</form>
    	</body>
    </html>
    Last edited by Cranford; 08-18-2008 at 06:18 PM. Reason: ...to allow blank fields

  • Users who have thanked Cranford for this post:

    PeaTearGriffin (08-18-2008)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •