This should do what you want.
I iterate over the selected options and count how many times each occurs. On the first detection of a duplicate I add an error message. I also make sure an email was selected. If there are any errors we alert them otherwise the form gets submitted.
(function ($) {
var form = $('#student_records').parent();
form.submit(function (event) {
event.preventDefault();
var selected = {},
errors = [];
form.find('option:selected').each(function (index, elem) {
var val = $(elem).val().trim();
if (! selected[val]) {
selected[val] = 1;
return;
} else if (selected[val] == 1) {
errors.push('Already selected: ' + $(elem).text());
}
selected[val] += 1;
});
if (! selected[1]) {
errors.push('Must select email.');
}
if (errors.length) {
alert(errors.join("\n"));
return;
}
form.submit();
});
}) (jQuery);
2
solved Check for value in drop down before submit