给Discuz X3.+ 帖子标题增加最少字数限制,并调整最大字数限制

前台设置

打开 static/js/common.js 文件,找到以下代码段落:

function strLenCalc(obj, checklen, maxlen) {
...
}

在该段代码后新增以下代码:

function strLenCalcBiaoti(obj, checklen, maxlen) {
    var v = obj.value, charlen = 0, maxlen = !maxlen ? 200 : maxlen, curlen = maxlen, len = strlen(v), minlen = 20;
    for(var i = 0; i < v.length; i++) {
        if(v.charCodeAt(i) < 0 || v.charCodeAt(i) > 255) {
            curlen -= charset == 'utf-8' ? 2 : 1;
            minlen -= charset == 'utf-8' ? 2 : 1;
        }
    }
    if(curlen >= len) {
        if(len < minlen) {
            $(checklen).innerHTML = "<font color=red>标题至少还应输入 " + Math.floor((minlen - len)/2) + " 字,须具有概括性。</font>";
        } else {
            $(checklen).innerHTML = "<font color=green>字数达标,最多还可输入 " + Math.floor((curlen - len)/2) + " 字,标题必须具有概括性。</font>";
        }
    } else {
        obj.value = mb_cutstr(v, maxlen, 0);
    }
}

其中 minlen = 20 的意思是最少要求输入20字节(20字节=10个汉字),可自行修改。

然后打开 forum/post_editor_extra 模板,找到以下代码片段:

<!--{else}-->
    <!--{if $_GET[action] != 'reply'}-->
        ...
    <!--{/if}-->            
    ...
    <script type="text/javascript">strLenCalc($('subject'), 'checklen', 80)</script>
<!--{/if}-->

以上整段替换为:

<!--{elseif in_array($_G['fid'], array(免限版块1,免限版块2,免限版块3))}-->
    <!--{if $_GET[action] != 'reply'}-->
        <span><input type="text" name="subject" id="subject" class="px" value="$postinfo[subject]" {if $_GET[action] == 'newthread'}onblur="if($('tags')){relatekw('-1','-1'{if $_G['group']['allowposttag']},function(){extraCheck(4)}{/if});doane();}"{/if} onkeyup="strLenCalc(this, 'checklen', 50);" style="width: 26em" tabindex="1" /></span>
    <!--{else}-->
        <span id="subjecthide" class="z">RE: $thread[subject] [<a href="javascript:;" onclick="display('subjecthide');display('subjectbox');$('subject').value='RE: {echo dhtmlspecialchars(str_replace('\'', '\\\'', $thread[subject]))}';display('subjectchk');strLenCalc($('subject'), 'checklen', 50);return false;">{lang modify}</a>]</span>
        <span id="subjectbox" style="display:none"><input type="text" name="subject" id="subject" class="px" value="" onkeyup="strLenCalc(this, 'checklen', 50);" style="width: 26em" /></span>
    <!--{/if}-->            
    <span id="subjectchk"{if $_GET[action] == 'reply'} style="display:none"{/if}>标题最多还可输入 <span id="checklen">50</span> 个字符</span>
    <script type="text/javascript">strLenCalc($('subject'), 'checklen', 50)</script>
<!--{elseif in_array($_G['groupid'], array(特殊用户组1,特殊用户组2,特殊用户组3))}-->
    <!--{if $_GET[action] != 'reply'}-->
        <span><input type="text" name="subject" id="subject" class="px" value="$postinfo[subject]" {if $_GET[action] == 'newthread'}onblur="if($('tags')){relatekw('-1','-1'{if $_G['group']['allowposttag']},function(){extraCheck(4)}{/if});doane();}"{/if} onkeyup="strLenCalcBiaoti(this, 'checklen', 80);" style="width: 26em" tabindex="1" /></span>
    <!--{else}-->
        <span id="subjecthide" class="z">RE: $thread[subject] [<a href="javascript:;" onclick="display('subjecthide');display('subjectbox');$('subject').value='RE: {echo dhtmlspecialchars(str_replace('\'', '\\\'', $thread[subject]))}';display('subjectchk');strLenCalcBiaoti($('subject'), 'checklen', 80);return false;">{lang modify}</a>]</span>
        <span id="subjectbox" style="display:none"><input type="text" name="subject" id="subject" class="px" value="" onkeyup="strLenCalcBiaoti(this, 'checklen', 80);" style="width: 26em" /></span>
    <!--{/if}-->            
    <span id="subjectchk"{if $_GET[action] == 'reply'} style="display:none"{/if}><span id="checklen">标题须含10~25个汉字,且具有概括性。</span></span>
    <script type="text/javascript">strLenCalcBiaoti($('subject'), 'checklen', 80)</script>
<!--{else}-->
    <!--{if $_GET[action] != 'reply'}-->
        <span><input type="text" name="subject" id="subject" class="px" value="$postinfo[subject]" {if $_GET[action] == 'newthread'}onblur="if($('tags')){relatekw('-1','-1'{if $_G['group']['allowposttag']},function(){extraCheck(4)}{/if});doane();}"{/if} onkeyup="strLenCalcBiaoti(this, 'checklen', 50);" style="width: 26em" tabindex="1" /></span>
    <!--{else}-->
        <span id="subjecthide" class="z">RE: $thread[subject] [<a href="javascript:;" onclick="display('subjecthide');display('subjectbox');$('subject').value='RE: {echo dhtmlspecialchars(str_replace('\'', '\\\'', $thread[subject]))}';display('subjectchk');strLenCalcBiaoti($('subject'), 'checklen', 50);return false;">{lang modify}</a>]</span>
        <span id="subjectbox" style="display:none"><input type="text" name="subject" id="subject" class="px" value="" onkeyup="strLenCalcBiaoti(this, 'checklen', 50);" style="width: 26em" /></span>
    <!--{/if}-->            
    <span id="subjectchk"{if $_GET[action] == 'reply'} style="display:none"{/if}><span id="checklen">标题须含10~25个汉字,且具有概括性。</span></span>
    <script type="text/javascript">strLenCalcBiaoti($('subject'), 'checklen', 50)</script>
<!--{/if}-->

其中“免限版块”处替换为不受限制的版块fid,“特殊用户组”处替换为限制更宽松(最长可输入80字节)的用户组id。其它用户则最多只能输入50字节。

接下来,打开 forum/post 模板,查找:

<script type="text/javascript" src="{$_G['setting']['jspath']}forum_post.js?{VERHASH}"></script>

替换为:

<!--{if in_array($_G['fid'], array(免限版块ID1,免限版块ID2,免限版块ID3))}--><script type="text/javascript" src="{$_G['setting']['jspath']}forum_post.js?{VERHASH}"></script><!--{else}--><script type="text/javascript" src="{$_G['setting']['jspath']}forum_post_xbttsd.js?{VERHASH}"></script><!--{/if}-->

static/js/forum_post.js 复制一份重命名为 forum_post_xbttsd.js 。打开 forum_post_xbttsd.js ,找到以下代码:

    if(($('postsubmit').name != 'replysubmit' && !($('postsubmit').name == 'editsubmit' && !isfirstpost) && theform.subject.value == "") || !sortid && !special && trim(message) == "") {
        showError('抱歉,您尚未输入标题或内容');
        return false;
    } else if(mb_strlen(theform.subject.value) > 80) {
        showError('您的标题超过 80 个字符的限制');
        return false;
    }

替换为:

    if(($('postsubmit').name != 'replysubmit' && !($('postsubmit').name == 'editsubmit' && !isfirstpost) && theform.subject.value == "") || !sortid && !special && trim(message) == "") {
        showError('您没有输入标题或正文');
        return false;
    } else if(mb_strlen(theform.subject.value) < 20 && mb_strlen(theform.subject.value) != 0) {
        showError('标题字数不足,且应具有概括性');
        return false;
    } else if(mb_strlen(theform.subject.value) > 80) {
        showError('标题字数过多,应简洁明了并具概括性');
        return false;
    }

确认将 forum_post_xbttsd.js 保存在 static/js/ 目录。

后台设置

打开 source/function/function_post.php 文件,查找:

    if(dstrlen($subject) > 80) {

替换为:

    if((in_array($_G['group']['groupid'], array(特殊用户组1,特殊用户组2,特殊用户组3)) && dstrlen($subject) > 80) || (!in_array($_G['group']['groupid'], array(特殊用户组1,特殊用户组2,特殊用户组3)) && dstrlen($subject) > 50) || ($_GET[action]=='newthread' && !in_array($_G['fid'], array(免限版块1,免限版块2,免限版块3)) && dstrlen(str_replace(array(" ","。。","??","!!",",,","、、","..","……","=","--"),'',$subject)) < 20)) {

以上代码含义为:如果用户组为1或2或3并且标题字符数大于80 或 用户组不为1或2或3并且标题字符数大于50 或 在非指定免限版块发布新帖、标题去掉空格、句号、问号、感叹号……后字符数小于20 ,提示 post_subject_toolong 。

打开 source/language/lang_message.php ,查找:

  'post_subject_toolong' => '抱歉,您的标题超过 80 个字符修改标题长度'

替换为:

  'post_subject_toolong' => '对不起,标题字数过多或过少,通常应为10~25个有效汉字,请修改。',

全部修改完成后,在免限制版块发帖时没有标题最少字数限制;在其它版块发帖时,标题有最少字数限制,且特殊用户组最多可输入80字符,其他用户为50字符。可根据你自己的需求修改。

本文链接:

https://www.wgrid.cn/28.html



评论功能已关闭,请直接邮箱联系