A Treelist is a combination of multi-list and tree fields. In this blog, we will explore how you can set up a custom validation for the field Treelist in Sitecore.
In general, "Treelist" type field allows the users to select n number of selection. But in some case, requirement will come to select two or three items only.
Please follow the below steps to achieve treelist field validation.
To achieve that go to your Template. Then select treelist field item.
Go to Validation field and put the regex "^({[^}]+}\|?){0,1}$" in the field.
In my case, this expression is limiting the selection to 1 only. But you can change any number you want in regex.
Now there is one problem that will occur if they use only regex. The user will not get a proper error message. The message will look like this.
This error message will confuse the user. To customize the message user, must put his customize message in the Validation Text field.
Now the error message will change while the user saves the item.
This way we can control the selection limit in Treelist type fields.
With the above given steps, you can set up custom validation for Sitecore Treelist.