Validate the dropdown field

Hi,

We have a lookup field in our SharePoint list. Which comes as a drop-down list and are set to select multiple items.

We want to limit the selection to maximum of 6 and from a predefined strings. If the items selected from the drop-down list greater than 6 or value selected is not from the set of strings we wanted then it should thrown a validation error. How do i do this?

Hello @Rinu,

You can use a custom validator to make sure that no more than six values are selected. Please see the code example below.
Please LookupFieldName with the internal name of the lookup field.
And you can specify any error text that will be displayed to a user, just replace ValidationText in the code.

fd.field('LookupFieldName').validators.push({
    name: 'ValidatorName',
    error: "ValidationText",
    validate: function(value) {
        if(value.length > 6){
            return false;
        }
            return true;
    }
});

And could you please clarify what do you mean under 'predefined strings'. What are the conditions? More details explanation and some screenshots would be helpful.

Thanks Mnikitina,

what i meant by predefined is there is a set of words that are allowed if user adds another it should throw a validation error.

@Rinu,

You can validate each selected value, please see the code sample below.

fd.field('LookupFieldName').validators.push({
        name: 'ValidatorName',
        error: "ValidationText",
        validate: function(value) {
            if(value.length > 0){
                for (var i = 0; i < value.length; i++){
                    if(value[i].LookupValue == 'General Counsel')
                      return false;
                }
            } 
             return true;
        }
    }); 

Also, you can filter lookup values to show only values that you want to be selected by a user. Please instructions in this article:
https://plumsail.com/docs/forms-sp/how-to/lookup-cascading.html

Thanks Mnikitina, It worked

1 Like