How to set up custom validation for the field Tree List in Sitecore?
Published: 19 May 2023

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.

Mitesh Patel - Technical Head - ADDACT
Sitecore || XMCloud || OrderCloud Certified
Mitesh, a distinguished Technical Head at Addact/Addxp, is a prominent figure in Sitecore/XMCloud/OrderCloud certified writing. From Sitecore XM Cloud Developer Certification to Sitecore 10 .NET Developer Certification and Sitecore OrderCloud Certification, Mitesh's expertise is unparalleled. Mitesh is not only a skilled Sitecore CMS developer but also a 12+ years experienced software engineer proficient in various technologies such as MVC, ASP.Net, C#, jQuery, and Azure cloud/AWS.