1

I applied one regex in the validation field to check if the entered characters in the RTE are less than 520 characters only, otherwise, it shows the error.

I've setup the validation as follows: enter image description here

But this regex is not matching exactly, even when I enter the 518 characters it throws the error (setup in the validation text field).

I thought that may be the regex is not correct but when I used the online regex tester tool then it is behaving correctly.

So, before raising it to the Sitecore team I thought to confirm here.

I also tried to use the existing validation rules in the /sitecore/system/Settings/Validation Rules/Field Rules/Sample/Max Length 40 and I tried this also but it was not working and I was unable to change the message that the client wants to see when string length doesn't match the criteria.

So, I am just confirming if someone has any solution to why regex not working or raise it with the Sitecore support team.

FYI: My environment is Sitecore 10.2 MCC with SXA

4
  • Validation regex works for me for RTE field. Have you counter spaces and html markup?
    – Marek Musielak
    Nov 29, 2022 at 8:55
  • Yes @MarekMusielak it has spaces and I can see that in the HTML tab of the RTE the HTML tags are also there. Nov 29, 2022 at 9:34
  • So when you say it's 518 characters, you mean 518 with spaces and html tags? That regex catches every character
    – Marek Musielak
    Nov 29, 2022 at 9:48
  • when I say 518 this means only characters. I did not include any HTML tags. Nov 29, 2022 at 11:55

1 Answer 1

3

I have had a go at implementing a validation rule that checks the field is less than 10 characters. Please try this out:

Duplicate an existing validation rule, that already uses sitecore RegexValidator.

These can be found here: /sitecore/system/Settings/Validation Rules/Field Rules/Common/

Name the item "Less than 10 Chracters" and give Title/Description.

Update Paramaters field to: Pattern=^.{0,10}$&Text=Field "{0}" must be less than 10 Characters.

Create new validator

Add the validation rule

Go to you field that you would like to validate and add the new validation action:

Adding a validation rule

Test that it works

Go to your item and add more than 10 characters into your field.

Press the validate button.

Validation rule being tested

3
  • I think your solution is working for me but is this right that this validation is counting the HTML tags' chars also? Nov 29, 2022 at 11:01
  • Hi - yes I believe it will be validating against the raw value for the field. So unfortunately it would count the html tags as well. If you wanted to make the count against the field (without tags) then I believe you would need to implement your own version of Sitecore.data.validators.RegexValidator. It could be a direct copy of the original, but prior to making the regex check, you could strip tags from the field. Nov 29, 2022 at 11:07
  • Fair enough. I am upvoting and accepting it. Nov 29, 2022 at 11:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.