-1

Im trying to prevent duplicated values on inputs with same name when I click a submit button, but it is not working and i am not sure why... I need some help to understand why is not working? Thanks lot in advance!

this is my code:

I tried with a solution I found here which it worked "on input change" behavior, but it doesnt with button click... My button:

<button type="button" id="approve" class="positive valid" tabindex="-1">Approve</button>

and my jquery

$('#received').on('click',function() {
    var $current = $(this);
    if ($('input[name^="RE_SignedByID"]').val() == $current.val() && $('input[name^="RE_SignedByID"]').attr('tabindex') !== $current.attr('tabindex') ) {
    alert('You can not have duplicated ID´s');
       return false;                        
    }else {
       return true;
    }
});

I want to show an alert and prevent the submit. Thanks a lot for any help!

Fernando Lopez
  • 31
  • 1
  • 10

2 Answers2

0

The issue is because you're comparing the value of the clicked button to the first input[name^="RE_SignedByID"] element.

To fix this you could instead create an array of all the input[name^="RE_SignedByID"] values using map(). You can then dedupe this list and compare the resulting array lengths. If they are different there was a duplicate. Try this:

$('#received').on('click', function(e) {
  var values = $('input[name^="RE_SignedByID"]').map(function() {
    return this.value.trim();
  }).get();  
  var unique =  [...new Set(values)];

  if (values.length != unique.length) {
    e.preventDefault();
    alert('You can not have duplicated ID\'s');
  }
});

Note that [...new Set(values)] will not work in IE. If you need to support legacy browsers there are plenty of alternatives to dedupe an array. See this answer for more information.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • I see! To get the values on an Array would be the soution I think, Im doing the checking of the values wrong, Im realizing that now... I'll make some changes – Fernando Lopez Feb 07 '19 at 11:00
  • can I have unique like this? var unique = $('input[name^="RE_SignedByID"]').val(); – Fernando Lopez Feb 07 '19 at 11:15
  • No, as calling `val()` on a collection of elements will only read the first. This is why I suggested using `map()` to read the values from *all* the elements in to a single array – Rory McCrossan Feb 07 '19 at 11:16
  • Allright :) Im trying to replace [...new Set(values)] cause it will be a problem if it does not work on IE. Thanks Rory! – Fernando Lopez Feb 07 '19 at 11:23
0

I could fix it! Here is the code... you can add a button event like:

$('#submit').on('click', function () { 


 var values = $('[name=RE_SignedByID]').map(function() {
                return this.value.trim();
                }).get(); 

                var values2= $('[name=RE_OwnersID]').map(function() {
                return this.value.trim();
                }).get(); 
                values.sort();
                values2.sort();
                for (var i = 0; i < values.length-1; i++) { 
                if( values[i] == values[i+1] && values[i] !=""){

                showAlert(translator.getTranslation(' You can not have duplicated signers ID\'s'));
                return false;
              //  break;
                }
                } 
                for (var i = 0; i < values2.length-1; i++) { 
                if( values2[i] == values2[i+1] && values2[i] !=""){

                showAlert(translator.getTranslation(' You can not have duplicated owners ID\'s'));
                return false;
              //  break;
                }
                }
 });
Fernando Lopez
  • 31
  • 1
  • 10