Textarea maxlength检查

时间:2010-04-08 12:18:08

标签: jquery html javascript

Textarea验证,

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

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

感谢.....

8 个答案:

答案 0 :(得分:13)

使用:

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

来自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');
     });
    });

答案 1 :(得分:9)

一个Google离开。

<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> 

编辑:

不冻结文字的代码:

<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>

答案 2 :(得分:5)

更简单的内联方法:

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

更改&#34; 100&#34;你需要多少个字符

答案 3 :(得分:2)

<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);
}

答案 4 :(得分:1)

我不知道内置的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;
}

答案 5 :(得分:1)

<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>

答案 6 :(得分:0)

使用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>

答案 7 :(得分:0)

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

<强> 1。将“maxLength”属性添加到textarea

<textarea cols="35" rows="3" maxLength="255" id="myTextArea" name="myTextArea">
</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));
            }
        }
    });
});