Javascript 文本区域最大长度检查

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2599688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:04:29  来源:igfitidea点击:

Textarea maxlength check

jqueryhtmljavascript

提问by Hulk

Textarea validation,

文本区域验证,

How to limit the characters in a textareafor not exceeding more than 50 characters.

如何限制 a 中的字符textarea不超过 50 个字符。

<textarea rows="5" cols="15"></textarea>

Thanks.....

谢谢.....

回答by zaf

Using:

使用:

<textarea rows="5" cols="15" maxlength="50"></textarea>

From http://sigswitch.com/2008/07/textarea-maxlength-with-jquery/:

http://sigswitch.com/2008/07/textarea-maxlength-with-jquery/

  $(document).ready(function(){
     $('textarea[maxlength]').keyup(function(){
      var max = parseInt($(this).attr('maxlength'));
      if($(this).val().length > max){
       $(this).val($(this).val().substr(0, $(this).attr('maxlength')));
      }

      $(this).parent().find('.charsRemaining').html('You have ' + (max - $(this).val().length) + ' characters remaining');
     });
    });

回答by Marcus L

One Google away.

一个谷歌了。

<script language="javascript" type="text/javascript">
<!--
function imposeMaxLength(Object, MaxLen)
{
  return (Object.value.length <= MaxLen);
}
-->
</script>

Implementation:
<textarea name="myName" onkeypress="return imposeMaxLength(this, 50);" ></textarea> 

EDIT:

编辑:

Code that doesn't freeze text:

不冻结文本的代码:

<script type="text/javascript">

/***********************************************
* Textarea Maxlength script- ? Dynamic Drive (www.dynamicdrive.com)
* This notice must stay intact for legal use.
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

function imposeMaxLength(obj){
var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
if (obj.getAttribute && obj.value.length>mlength){
  obj.value=obj.value.substring(0,mlength)
}

</script>

<textarea maxlength="40" onkeyup="return imposeMaxLength(this)"></textarea>

回答by Dss

Much simpler inline method:

更简单的内联方法:

<textarea cols="60" rows="5" onkeypress="if (this.value.length > 100) { return false; }"></textarea>

Change the "100" to however many characters you want

将“100”更改为您想要的任意字符数

回答by Surinder

<asp:TextBox ID="txtColumn2" runat="server" TextMode="MultiLine" MaxLength="500" onkeyDown="checkTextAreaMaxLength(this,event,'500');"
onblur="onBlurTextCounter(this,'500');"></asp:TextBox>


function checkTextAreaMaxLength(textBox, e, maxLength) {
    if (!checkSpecialKeys(e)) {
        if (textBox.value.length > maxLength - 1) {
            if (window.event)//IE
                e.returnValue = false;
            else//Firefox
                e.preventDefault();
        }
    }
    onBlurTextCounter(textBox, maxLength);
}

function checkSpecialKeys(e) {
    if (e.keyCode != 8 && e.keyCode != 46 && e.keyCode != 37 && e.keyCode != 38 && e.keyCode != 39 && e.keyCode != 40)
        return false;
    else
        return true;
}

function onBlurTextCounter(textBox, maxLength) {
    if (textBox.value.length > maxLength)
        textBox.value = textBox.value.substr(0, maxLength);
}

回答by Dror

I don't know of a built in HTML way but you can use this:

我不知道内置的 HTML 方式,但您可以使用它:

<textarea rows="5" cols="15" onkeydown="return validateCharLimit(this);"></textarea>??????????????

function validateCharLimit(area) 
{
    var limit = 50;
    var iChars = area.value.length;
    if (iChars > limit) {
        return false;
    }
    return true;
}

回答by nik

<script>
function chkLen()
{
var tlen=document.getElementById('myTA').value.length;
if(tlen>50)
{
document.getElementById('myTA').value.substr(0,49)
}
}
</script>

<body>
    <textarea id="myTA" onkeyup='chkLen()'></textarea>
</body>

回答by Fawad Ghafoor

Limit Number of Characters in a TextArea using jQuery

使用 jQuery 限制 TextArea 中的字符数

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
   <title>Limit Number of Characters in a TextArea</title>
    <script type='text/javascript' src='http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js'></script>
  <script type='text/javascript'>
    $(document).ready(function() {
        $('#ta').keyup(function() {
            var len = this.value.length;
            if (len >= 50) {
                this.value = this.value.substring(0, 50);
            }
            $('#charLeft').text(50 - len);
        });
    });
  </script>
</head>
<body>
<textarea id="ta" cols="15" rows="5"></textarea><br/>
  (Maximum characters: 50)<br/>
  <span id="charLeft"> </span>  Characters left
</body>

回答by detale

Here's a solution utilizaing HTML 5 textarea's "maxLength" attribute and jQuery 1.7+.

这是一个利用HTML 5 textarea 的“maxLength”属性和 jQuery 1.7+的解决方案。

1. Add "maxLength" attribute to textarea

1.为textarea添加“maxLength”属性

<textarea cols="35" rows="3" maxLength="255" id="myTextArea" name="myTextArea">
</textarea>

2. Add jQuery event handler for the textarea

2.为textarea添加jQuery事件处理程序

$(function() {
    $('#myTextArea').on('input propertychange', function () {
        var propMaxLength = $(this).prop('maxLength');
        if (!propMaxLength || typeof propMaxLength != 'number') {
            var maxLength = $(this).attr('maxlength'), txt = $(this).val();
            if (txt.length > maxLength) {
                $(this).val(txt.substr(0, maxLength));
            }
        }
    });
});