0

我正在使用CKEditor(以及CKFinder)。在存储到数据库之前,我需要验证它呈现的内容。

如果 CKEditor 呈现的内容为空白,则应显示验证违规消息。这可以在服务器端轻松完成,但是如果用户输入空格和换行符,则应将其删除,并且不应将违反验证规则的内容插入数据库。

由于 CKEditor 呈现 HTML,因此仅在服务器端使用(关于 Java)之类的函数是不可能的。String.trim()如果我只输入一些空格和换行符,那么呈现的 HTML 如下所示。

<p>            
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   
</p>

<p>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</p>

它实际上代表了一些空格和换行符,它们不能通过简单地使用服务器端函数来修剪。那么在这种情况下执行验证的方法是什么?

4

2 回答 2

0

我是一个巨大的书呆子,并为此提出了一个小建议。基本上得到CKEditor的值,把它吐进一个一次性的DIV,用jQuery得到那个内容的长度,然后和0比较。

我制作了一个jsfiddle示例,它同时为 CKFinder 和一次性使用 contenteditable DIV。这是JavaScript,我希望你明白。如果没有,请提出问题,我将尝试澄清我扭曲的代码。

$(function() {
    // this just keeps track of the contenteditable
    // replace with your submit or whatever event
    $('body').on('focus', '[contenteditable]', 
            function() {
                var $this = $(this);
                $this.data('before', $this.html());
                return $this;
            }).on('blur keyup paste', '[contenteditable]', function() {
                var $this = $(this);
                if ($this.data('before') !== $this.html()) {
                    $this.data('before', $this.html());
                    $this.trigger('change');
                }
                return $this;
            });
    $('body').on('change', function() {
       validate();
    });

    // This actually validates the string as 0-rendering or not.
    function validate() {

        // Replace this text getter with getting the CKE content
        // Then vomit it into a DIV like check and then get it again.
        // just keep #check invisible with CSS.
        var text = $('#check').text();
        var l = text.trim().length;

        // if length is 0, it renders as whitespace only
        // I show a message to whine about that
        if (l === 0) {
            $('#aaa').text("DOES NOT PASS");    
        }
        else {
             $('#aaa').text("PASS");   
        }   
    }
    validate();
});
于 2013-10-23T11:17:07.847 回答
0

如果它返回 true,则只需将文本编辑器字符串传递给此方法,这意味着我们有没有实际文本内容的空字符串。

public boolean validateSpace(String str) {
        if (str != null) {
            String replaceStr = str.replace("&nbsp;", "").replace("<p>", "").replace("</p>", "").replace("<br>", "").replace("<br />", "");
            if (replaceStr == null || replaceStr.trim().isEmpty()) {
                return true;
            }
        }
        return false;
    }
于 2020-06-05T10:19:37.237 回答