如何限制用户可以输入 ckeditor 的字符数?

发布于 2024-09-07 05:39:24 字数 156 浏览 5 评论 0原文

我正在使用 CKeditor,我需要能够对其施加 maxLength 限制。 例如,阻止用户输入超过 100 个字符,不包括 html 字符 由用户应用。

有人能够做到这一点吗?

谢谢,如果您向我指出资源,我将不胜感激。我在这里发现了类似的问题,但它们没有多大帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心的憧憬 2024-09-14 05:39:24

我怀疑即使有人发布了一种方法,这最终是否会可靠。考虑以下内容:

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

这应该与大多数标签相匹配,但是如果有人做了这样的奇怪事情怎么办:

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

现在,应该忽略标签的正则表达式错误地识别了该图像的 alt 标签的内容,并将其计入其字符限制。如果某些后端系统要求文本内容仅为 100 个字符,我建议做的是为用户提供最大长度为 100 的单个文本输入,然后寻找另一个控件或库来让他们更改其外观和通过CSS感受。

尝试去掉 HTML 标签然后计算剩余的字符除了让您头疼之外不会做任何事情,在最好的情况下很容易出错,在最坏的情况下会完全出现故障。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文