views:

24

answers:

1

Hi, I'm using the CKeditor and I need to be able to impose a maxLength restriction on it. For instance, prevent user from entering more than 100 characters, excluding the html characters applied by the user.

Has anyone been able to do this?

Thanks, I'd appreciate if you point me towards a resource. I found similar questions here but they were not of much help.

A: 

I doubt this is going to end up being reliable even if someone posts an approach. Consider the following:

var tags = /<[^>]*?\/?>/;

That should match most tags, but what if you get someone who does something screwy like this:

<img alt=">My Title<" />

Now your regular expression that should be ignoring tags is improperly recognizing the contents of this image's alt tag as counting towards their character limit. If some back end system requires that the text content be only 100 characters what I'd suggest doing is giving the user a single text input with a maxlength of 100, and then look for another control or library that will let them change it's look and feel via CSS.

Attempting to strip out the HTML Tags and then count the remaining characters is unlikely to do anything but give you a headache, will be error prone in the best of cases, and will malfunction entirely in the worst of cases.

g.d.d.c
I wrote a custom validator for this. The drawback is that - it will allow you to enter over the allowed text limit but will not allow you to submit it as the validator flags it as an invalid submission.
Mallika Iyer
the validator strips the html and counts only the actual text so the html was not an issue.
Mallika Iyer