I have six input fields where a user enters text strings. These strings cannot be identical so I need to warn the user as soon as duplicate entries have been attempted. I have the following jQuery which nicely alert's the user when there has been duplicate entry. However, I'm trying to remove the input field that was the duplicate, and this code actually removes the first occurrence of the duplicated string. Is it possible to alert the user there has been a duplicate entry and also remove the correct duplicate input field? Thanks for your help
HTML:
<input type="text" class="form-control alldifferent" id="barcode0" name="barcode0" placeholder="barcode 1">
<input type="text" class="form-control alldifferent" id="barcode1" name="barcode1" placeholder="barcode 2">
<input type="text" class="form-control alldifferent" id="barcode2" name="barcode2" placeholder="barcode 3">
<input type="text" class="form-control alldifferent" id="barcode3" name="barcode3" placeholder="barcode 4">
<input type="text" class="form-control alldifferent" id="barcode4" name="barcode4" placeholder="barcode 5">
<input type="text" class="form-control alldifferent" id="barcode5" name="barcode5" placeholder="barcode 6">
jQuery:
$(".alldifferent").keyup(function() {
var val = $(this).val();
if (val != '') {
$(".alldifferent").not(this).each(function() {
if ($(this).val() == val) {
alert("Error, Duplicate Barcode " + val);
val = $(this).val('');
return false; // stop the loop
}
});
}
});
Fiddle: click here