This topic is locked
[SOLVED]

Prevent duplicate value in more than one dropdownl boxes

9/12/2024 4:39:01 PM
PHPRunner General questions
A
Abbas author

Hi

I have 10 dropdownl boxes (names: wish1 to wish10) on page , I want the value not to be repeated in the rest of the dropdown boxes

This java code was written by ChatGPT:

function checkWishes() {
// Retrieve values from all input fields
const wishes = [];
for (let i = 1; i <= 10; i++) {
const wish = document.getElementById('wish' + i).value.trim();
if (wish) {
wishes.push(wish);
}
}

// Create a Set to filter unique values and check for duplicates
const uniqueWishes = new Set(wishes);
if (uniqueWishes.size < wishes.length) {
document.getElementById('result').innerText = 'There are duplicate wishes!';
} else {
document.getElementById('result').innerText = 'All wishes are unique!';
}
}

another jquery code

$(document).ready(function() {
$('#checkDuplicates').on('click', function() {
let selectedValues = [];
let duplicates = [];

// Iterate through each wish dropdown
for (let i = 1; i <= 5; i++) { // Adjust the loop according to the number of dropdowns
let value = $('#wish' + i).val();
if (value) {
// If the value is already in selectedValues, add it to duplicates
if (selectedValues.includes(value)) {
duplicates.push(value);
} else {
selectedValues.push(value);
}
}
}

// Remove duplicate entries from duplicates array
duplicates = [...new Set(duplicates)];

if (duplicates.length > 0) {
$('#result').html('Duplicate wishes found: ' + duplicates.join(', '));
} else {
$('#result').html('No duplicates found.');
}
});
});

How can this code be applied to check that the values ​​selected by the user are not repeated when pressing the submit button?

C
cristi 9/13/2024

See my suggestion below - and you don't need to adjust the number of dropdowns in loops with this code:

Inspired from here:

In javascript onload event for add/edit pages with the dropdowns you put this:

this.on('beforeSave', function(formObj, fieldControlsArr, pageObj){
var selects = document.getElementsByTagName('select');
var values = [];
for(i=0;i<selects.length;i++) {
var select = selects[i];
if(values.indexOf(select.value)>-1) {
alert('Please select unique values for each dropdown!');
Runner.delDisabledClass ( pageObj.saveButton );
return false;
}
else
values.push(select.value);
}
});
A
Abbas author 9/13/2024

Thank you, your suggestion was great and solved the problem

I made a small modification to accept empty fields

this.on('beforeSave', function(formObj, fieldControlsArr, pageObj){
var selects = document.getElementsByTagName('select');
var values = [];

for (var i = 0; i < selects.length; i++) {
var select = selects[i];
var selectValue = select.value;

// Allow empty string and only check for duplicates if the value is not empty
if (selectValue !== "" && values.indexOf(selectValue) > -1) {
swal ( "Error", "Please select unique values for each dropdown!." , "error" );
Runner.delDisabledClass(pageObj.saveButton);
return false;
} else if (selectValue !== "") {
values.push(selectValue);
}
}
});