How to detect the duplication of values of input fields ?

Suppose you are making a registration form in which you are taking phone numbers of users. Now, You want to ask the user for four alternative contact numbers. You want to make sure that all four contact numbers given by users should be different. 

This functionality can be implemented in two ways :

  1. At the time of input before submitting form: For this, we can use “oninput” event provided by HTML itself which triggers the function specified in javascript whenever input changes in input field
  2. After submitting form: For this, we can use “onsubmit” or “onclick” event which is also provided by HTML and they work on Buttons.  “onclick” event will work for buttons while “onsubmit” event will work for forms 

Below examples illustrate the both approaches:

Example 1:

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    <head>
    <script>
    function gfg_check_duplicates() {
       var myarray = [];
       for (i = 0; i < 4; i++) {
         myarray[i] = 
          document.getElementById("gfg_field" + i).value;
                }
         for (i = 0; i < 4; i++) {
            for (j = i + 1; j < 4; j++) {
               if (i == j || myarray[i] == "" || myarray[j] == "") 
                    continue;
                   if (myarray[i] == myarray[j]) {
                   document.getElementById("status" + i)
                     .innerHTML = "duplicated values!";
                   document.getElementById("status" + j)
                    .innerHTML = "duplicated values!";
                        }
                    }
                }
            }
        </script>
    </head>
    <body>
        Ph no1:
        <input id="gfg_field0" 
               oninput="gfg_check_duplicates()" />
        <div id="status0"></div>
        <br />
        Ph no2:
        <input id="gfg_field1" 
               oninput="gfg_check_duplicates()" />
        <div id="status1"></div>
        <br />
        Ph no3:
        <input id="gfg_field2" 
               oninput="gfg_check_duplicates()" />
        <div id="status2"></div>
        <br />
        Ph no4:
        <input id="gfg_field3" 
               oninput="gfg_check_duplicates()" />
        <div id="status3"></div>
        <br />
    </body>
</html>

chevron_right


Output: 



Example 2: In this example, we will show which of the values are same and works dynamically as we change input.

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  <head>
    <script>
    function gfg_check_duplicates() {
     var myarray = [];
     for (i = 0; i < 4; i++) {
      document.getElementById("status" + i).innerHTML = "";
      myarray[i] =
      document.getElementById("gfg_field" + i).value;
         }
     for (i = 0; i < 4; i++) {
      var flag = false;
        for (j = 0; j < 4; j++) {
          if (i == j || myarray[i] == "" || myarray[j] == "") 
             continue;
          if (myarray[i] == myarray[j]) {
             flag = true;
           document.getElementById("status" + i).innerHTML += 
             "<br>Its identical to the phone number " + (j + 1);
                        }
                    }
          if (flag == false) 
            document.getElementById("status" + i).innerHTML = "";
                }
            }
        </script>
    </head>
    <body>
        <h4>Phone no. 1</h4>
        <input id="gfg_field0" 
               oninput="gfg_check_duplicates()" />
        <div id="status0"></div>
        <h4>Phone no. 2</h4>
        <input id="gfg_field1" 
               oninput="gfg_check_duplicates()" />
        <div id="status1"></div>
        <h4>Phone no. 3</h4>
        <input id="gfg_field2" 
               oninput="gfg_check_duplicates()" />
        <div id="status2"></div>
        <h4>Phone no. 4</h4>
        <input id="gfg_field3" 
               oninput="gfg_check_duplicates()" />
        <div id="status3"></div>
        <br />
    </body>
</html>

chevron_right


Output:

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  <head>
    <script>
       function gfg_check_duplicates() {
         var myarray = [];
         for (i = 0; i < 4; i++) {
         document.getElementById("status" + i).innerHTML = "";
         myarray[i] = 
         document.getElementById("gfg_field" + i).value;
         }
         for (i = 0; i < 4; i++) {
          var flag = false;
          for (j = 0; j < 4; j++) {
            if (i == j || myarray[i] == "" || myarray[j] == "") 
                continue;
            if (myarray[i] == myarray[j]) {
                flag = true;
             document.getElementById("status" + i).innerHTML += 
             "<br>Its identical to the phone number " + (j + 1);
                        }
                    }
             if (flag == false) 
          document.getElementById("status" + i).innerHTML = "";
                }
            }
        </script>
    </head>
    <body>
        <h4>Phone no. 1</h4>
        <input id="gfg_field0" />
        <div id="status0"></div>
        <h4>Phone no. 2</h4>
        <input id="gfg_field1" />
        <div id="status1"></div>
        <h4>Phone no. 3</h4>
        <input id="gfg_field2" />
        <div id="status2"></div>
        <h4>Phone no. 4</h4>
        <input id="gfg_field3" />
        <div id="status3"></div>
        <br>
        <button type="submit" onclick="gfg_check_duplicates()">
          Check for Duplicates !
        </button>
    </body>
</html>

chevron_right


Output:

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.