【问题标题】:How to limit the number of characters and lines in the textbox using javascript?如何使用javascript限制文本框中的字符数和行数?
【发布时间】:2014-12-12 08:18:34
【问题描述】:
<script type="text/javascript">
function checking(textarea, maxLines, maxChar) {
    var lines = textarea.value.replace(/\r/g, '').split('\n'), lines_removed, char_removed, i;
    if (maxLines && lines.length > maxLines) {
        lines = lines.slice(0, maxLines);
        lines_removed = 1
    }
    if (maxChar) {
        i = lines.length;
        while (i-- > 0) if (lines[i].length > maxChar) {
            lines[i] = lines[i].slice(0, maxChar);
            char_removed = 1
        }
        if (char_removed || lines_removed) {
            textarea.value = lines.join('\n')
        }
    }
}
</script>

这是我根据之前的几篇文章使用的。但是这段代码允许我在“m”行中输入“n”个字符。有人可以帮我写一个只需要 30 个字符和 2 行的代码吗? {假设如果第一行包含 10 个字符,则下一行应包含 20 个字符,但总字符数应为 30,行数为 2。}

【问题讨论】:

  • 你也可以发布你的 HTML 吗?和 var lines = textarea.value.replace(/\r/g, '').split('\n'), lines_removed, char_removed, i;似乎有一些函数调用,但你写的不正确
  • stackoverflow.com/questions/556767/… 基于这篇文章我得到了这个代码。 @Alok
  • 另请注意,如果您没有对文本框长度进行服务器端检查,这可能涉及安全问题

标签: javascript html


【解决方案1】:

希望下面的代码可以节省您的时间。

在页眉中插入以下 JavaScript 函数:

<script language="javascript" type="text/javascript">
function limitText(limitField, limitCount, limitNum) { 
    if (limitField.value.length > limitNum) { 
        limitField.value = limitField.value.substring(0, limitNum);
    } else {
        limitCount.value = limitNum - limitField.value.length;
    }
}
</script>

在页面正文中插入以下代码:

<form name="myform">
    <input name="limitedtextfield" type="text" onKeyDown="limitText(this.form.limitedtextfield,this.form.countdown,15);" 
    onKeyUp="limitText(this.form.limitedtextfield,this.form.countdown,15);" maxlength="15"><br>
    <font size="1">(Maximum characters: 15)<br>
    You have <input readonly type="text" name="countdown" size="3" value="15"> characters left.</font>
</form>

此解决方案仅用于使用 Java Script 限制文本框中的字符数。

【讨论】:

  • 谢谢。我已经尝试过了。我需要的是允许 textarea 只输入 30 个字符和 两行。@Ye Win
  • codeproject.com/Articles/56392/… 这个链接会给你很多我想。我的测试很好。 @lazyCat
【解决方案2】:

jQuery 文件:

jquery.maxlength.js

/*
             * jQuery Maxlength plugin 1.0.0
             *
             * Copyright (c) 2013 Viral Patel
             * http://viralpatel.net
             *
             * Licensed under the MIT license:
             *   http://www.opensource.org/licenses/mit-license.php
*/


   ;(function ($)
   {

          $.fn.maxlength = function(){

             $("textarea[maxlength], input[maxlength]").keypress(function(event)
             { 
                var key = event.which;

               //all keys including return.
               if(key >= 33 || key == 13 || key == 32) {
                  var maxLength = $(this).attr("maxlength");
                  var length = this.value.length;
                if(length >= maxLength) 
                {                    
                    event.preventDefault();
                }
           }
        });
      }

   })(jQuery);

在您的主文件中

<script type="text/javascript">
        $(document).ready(function($){
            $().maxlength();
        });
</script>

【讨论】:

    猜你喜欢
    • 2019-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多