javascript - to check duplicate values for text box with same id onkeyup event -


please validate duplicate values text box having same id in loop onkeyup or onblur event, need validate before submitting form,

n number of textbox generated in loop, in each text box need enter rank, thereby need validate duplicate rank should not entered after value entered text box,

enter code here

       <%iterator iterator_t = tem.keyset().iterator();    int =0;    while (iter.hasnext()) {        string key_t = (string) iter.next();             modulebean modulebean = (modulebean) tem.get(key_t); %>            <input type="text" name="rank1" id="rank1" size="2" "/>          <%}%>    

you can not use same id it, instead add class it. demo

<input type="text" name="rank1" id="rank1" size="2" class="rank" value="1"/> <input type="text" name="rank2" id="rank2" size="2" class="rank" value="2"/>  function find_duplicates(arr) {   var len=arr.length,       out=[],       counts={};    (var i=0;i<len;i++) {     var item = arr[i];     var count = counts[item];     counts[item] = counts[item] >= 1 ? counts[item] + 1 : 1;   }    (var item in counts) {     if(counts[item] > 1)       out.push(item);   }    return out; }  $('.rank').keyup(function() {     var listofvalues = [];      $('.rank').each(function()     {         if($(this).val()!='')            listofvalues.push($(this).val());     });      var duplicates = find_duplicates(listofvalues);      if(duplicates.length>0)     {         $('#result').html('duplicates are:');         $('#result').append(json.stringify(duplicates));     }     else     {         $('#result').html('no duplicates found');     } }); 

Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -