I'm trying to check if there are duplicated values for specific input field which is repeating inside each containing element.
The filed should be considered as a "duplicate" only if has the same value with other comparing field(s) and sharing the same parent container, which means the field can have the same value in 2 or more different containers. I now it sounds a bit confusing but I will add the markup and the current jQuery code so hopefully it will be more clear.
var attendees_group = $('.attendee-accordion-group'),
inputs = attendees_group.find('input[name*="_attendeeemail"]');
inputs.change(function(e) {
//Create array of input values
var ar = inputs.map(function() {
if ($(this).val() != '') return $(this).val()
}).get();
//Create array of duplicates if there are any
var unique = ar.filter(function(item, pos) {
return ar.indexOf(item) != pos;
});
console.log(unique.length);
//if duplicates
if (unique.length != 0) {
//do something
}
});
body {
margin: 0;
padding: 0;
font-family: Arial, Helvetica, sans-serif;
font-size: 14px
}
h3 {
display: block;
margin: 0 0 15px 0;
font-size: 18px;
}
h4 {
padding: 0;
margin: 0 0 15px 0;
font-size: 15px;
}
form {
padding: 20px;
}
.attendee-accordion-group {
padding: 40px;
border: 1px solid #999;
margin-bottom: 20px;
}
input {
border: 1px solid #ccc;
width: 50%;
line-height: 1;
height: 20px;
}
.button {
display: inline-block;
background: #0274be;
color: #fff;
padding: 10px 30px;
text-decoration: none;
border: none;
cursor: pointer;
margin-bottom: 20px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<form>
<h3>Event Title 1</h3>
<div class="attendee-accordion-group">
<h4>Attendee 1</h4>
<div class="attendee-panel">
<p>
<label for="field_attendeename_1__1" class="">Name</label>
<span class="input-wrapper">
<input type="text" class="input-text" name="field_attendeename_1__1" id="field_attendeelastname_1__1" value="">
</span>
</p>
<p>
<label for="field_attendeeemail_1__1" class="">Email</label>
<span class="input-wrapper">
<input type="text" class="input-text" name="field_attendeeemail_1__1" id="field_attendeeemail_1__1" value="">
</span>
</p>
</div>
<h4>Attendee 2</h4>
<div class="attendee-panel">
<p>
<label for="field_attendeename_1__2" class="">Name</label>
<span class="input-wrapper">
<input type="text" class="input-text" name="field_attendeename_1__2" id="field_attendeelastname_1__2" value="">
</span>
</p>
<label for="field_attendeeemail_1__2" class="">Email</label>
<span class="input-wrapper">
<input type="text" class="input-text" name="field_attendeeemail_1__2" id="field_attendeeemail_1__2" value="">
</span>
</p>
</div>
<h4>Attendee 3</h4>
<div class="attendee-panel">
<p>
<label for="field_attendeename_1__3" class="">Name</label>
<span class="input-wrapper">
<input type="text" class="input-text" name="field_attendeename_1__3" id="field_attendeelastname_1__3" value="">
</span>
</p>
<p>
<label for="field_attendeeemail_1__3" class="">Email</label>
<span class="input-wrapper">
<input type="text" class="input-text" name="field_attendeeemail_1__3" id="field_attendeeemail_1__3" value="">
</span>
</p>
</div>
</div>
<h3>Event Title 2</h3>
<div class="attendee-accordion-group">
<h4>Attendee 1</h4>
<div class="attendee-panel">
<p>
<label for="field_attendeename_2__1" class="">Name</label>
<span class="input-wrapper">
<input type="text" class="input-text" name="field_attendeename_2__1" id="field_attendeelastname_2__1" value="">
</span>
</p>
<p>
<label for="field_attendeeemail_2__1" class="">Email</label>
<span class="input-wrapper">
<input type="text" class="input-text" name="field_attendeeemail_2__1" id="field_attendeeemail_2__1" value="">
</span>
</p>
</div>
<h4>Attendee 2</h4>
<div class="attendee-panel">
<p>
<label for="field_attendeename_2__2" class="">Name</label>
<span class="input-wrapper">
<input type="text" class="input-text" name="field_attendeename_2__2" id="field_attendeelastname_2__2" value="">
</span>
</p>
<p>
<label for="field_attendeeemail_2__2" class="">Email</label>
<span class="input-wrapper">
<input type="text" class="input-text" name="field_attendeeemail_2__2" id="field_attendeeemail_2__2" value="">
</span>
</p>
</div>
</div>
<button type="button" class="button">Next</button>
</form>
<div class="error">
</div>
OK, so, the container is a div with a class .attendee-accordion-group
, and inside, each text field labeled email, should be unique. I hope this clarifies issue a bit.
I used the js code for comparing field values from this answer
Right now all the "email" fileds, no matter of container are duplicates if values are the same, I was wondering if someone can help me with this.