Expand my Community achievements bar.

Validation on drop-down fields to check for duplicates

Avatar

Level 1

I am working on a form that asks employees to select up to five disciplines from a list of available disciplines.  I currently have the dynamic form set to show one drop-down field (called Primary Discipline) that contains the list of disciplines, plus another drop-down field (called Additional Discipline) with the same list of disciplines.  I basically copied and pasted the original text entry field (Primary Discipline) that contained the list of drop-down values, to the other Additional Disciplines text entry field (to avoid having to enter in the long list of values again - I don't want it to be a global field because I was under the impression a global field would display the same value in all other similarly named fields - I need distinct values in each Discipline field).

That being said, I want some validation on the drop-down fields.  An employee is given the choice of selecting UP to five disciples (so on the button that spawns a new line to add a discipline, I already have it configured to only allow up to 4 lines since there is already an initial primary discipline line).  They are also not supposed to select a discipline more than once.  For example, they shouldn't be able to select Administrative as their Primary Discipline and then also have Administrative listed as another additional discipline.

Is there any way to validate this using JavaScript?  How would I compare the different drop-down fields among each other to check for duplicates?

I have attached the whole form for reference (FYI it's a work in progress - the drop-lists I am referring to are in the second to last subform called 'DisciplineEntry' and they are in the subform group called 'Disciplines').

2 Replies

Avatar

Former Community Member

The solution depends upon the use of value bindings to the text properties of each drop-down. The following code works when there are NO values specified in the Binding tab of each drop-down.

// form1.page1.subform1.lunch::change - (JavaScript, client)

if (xfa.event.newText == form1.page1.subform1.dinner.rawValue) {
    xfa.host.messageBox("Are you sure you want " + xfa.event.newText + " again?");
}

// form1.page1.subform1.dinner::change - (JavaScript, client)

if (xfa.event.newText == form1.page1.subform1.lunch.rawValue) {
    xfa.host.messageBox("Are you sure you want " + xfa.event.newText + " again?");
}

If you need to use value binding the solution would differ.

Steve

Avatar

Level 10

Hi,

Steve's solution is definitely safer!!!! The following will delete the previously chosen item from the following dropdown list.

If you were to bind the values for each drop down list, starting with 0 up to 52, then the following Javascript in the exit event of the first dropdown:

var sDiscipline = this.rawValue;

xfa.host.resetData("xfa.form.form1.Main.Disciplines.DisciplineEntry.DiscEntryTable.AddDisc.PickDiscipline"); //second dropdown

form1.Main.Disciplines.DisciplineEntry.DiscEntryTable.AddDisc.PickDiscipline.deleteItem(sDiscipline);

This works for the first "additional discipline", a routine would be needed for added rows.

The danger with this is that if the user goes back and makes re-selects / makes a different from the first dropdown, then it deletes another option from the second dropdown. You could end up with no options if the user kept going back to the previous drop down and making changes.

Good luck,

N.