I overheard some colleagues on work today speaking about that they wanted to limit the length of the URLs on the pages in EPiServer.

I got curious of how to do that so did some research and hacked together a proof of concept :)

It's really easy to implement validation in EPiServer, here's my take on it:

//Using the AlloyProject, all pages inherits from SitePageData
//This code will be running on all pages when editing.
public class PageNameValidation : IValidate<SitePageData>
    IEnumerable<ValidationError> IValidate<SitePageData>.Validate(SitePageData page)
        var fullPath = !string.IsNullOrEmpty(page.ExternalURL) ? page.ExternalURL : UrlResolver.Current.GetUrl(page.ContentLink);
        var maxUrlLength = 20;

        if (fullPath.Length > maxUrlLength)
            return new[]
                new ValidationError()
                    ErrorMessage =
                            string.Format("The length of the URL exceeds the recommendation of maximum {0} chars. Currently it's {1} characters long: {2}",
                                maxUrlLength, fullPath.Length, fullPath),
                    PropertyName = "PageName",
                    Severity = ValidationErrorSeverity.Error,
                    ValidationType = ValidationErrorType.Unspecified

        return new ValidationError[0];

Im retrieving the url by checking if a Simple address exists, if not im using the urlresolver to get the relative path.

Personally I don't really see the need of limiting the lengths of the urls so I would probably go with a severity level of Warning/Information instead of Error.

It looks like this in edit mode when trying to publish the page. You can still publish the page, it's just a warning.

If you set the severity level to Error, the editor will not be able to publish the page.

This was just a quick writeup, you can do all kind of validations when editing pages, you can read more about validation here