SaaS CMS has officially launched! Learn more now.

Is there a way to give a character limit to XHTMLString and strings?



At the moment our client is experiencing an issue when translating content using a third party plugin, which says that "The field Title must be a string or array type with a maximum length of '50'"

Is there any Data Annotation that i could use on a XHTMLString/String or would i need to create my own Validation method?


Edited, Oct 13, 2023 12:42

You can use System.ComponentModel.DataAnnotations.StringLengthAttribute for string properties. But for XhtmlString properties you would need to roll your own validator.

Oct 14, 2023 6:01

I've blogged on this a while ago,

It'll enable you to have a minimum amount of characters as well as a maximum amount.

[Display(Name = "MainContent")]
[XhtmlStringLengthValidation(50, 500, CustomErrorMessage = "The Main content field must contain more than {0} characters but less than {1}.")]
public virtual XhtmlString MainContent{ get; set; }

It should as well only count text characters not including the markup.

Oct 16, 2023 8:45
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.