Search code examples
jquerycomparisoneach

Compare input values using jQuery


I have a form with 3 input file fields. I want to make sure that they do not upload the same document twice. Right now my code is as follows:

$(':input.File').each(function() {
    if ($(this).val() == T) {
        $this.after('<br/><span class="error">Duplicate</span>');
    }
})

The code works but my issue is that it always throws the error because it is comparing against itself. It identifies all file fields when I want it to compare all file fields but itself.

Is there a way to exclude the field being compared against? Essentially I want to check all the file field value except for the $(this) field.


Solution

  • If you know there is always going to be at least one duplicate, why not just allow one duplicate?

    i = 0;
    $(':input.File').each(function() {
        if ($(this).val() == T && i != 0) {
            $this.after('<br/><span class="error">Duplicate</span>');
        }
    
        i++;
    })