限制文本区域

发布于 2024-12-08 08:05:19 字数 301 浏览 0 评论 0原文

可能的重复:
限制文本区域中输入的字符数

如何设置限制文本区域可以输入的字符数?我有一个文本区域,您最多可以在其中输入 50 个字符。我怎样才能用Javascript做到这一点。我希望输入也是字符,这样人们就不会输入大量输入

Possible Duplicate:
limit number of characters entered in textarea

How can I set a limit on the number of characters possible to go into a textarea? I have a textarea that you can put max 50 characters into it. How can i make this with Javascript. I want that the enters are also characters so people wont put lots of enters.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦在深巷 2024-12-15 08:05:19

上面带有 onkeypress 的代码不适用于复制/粘贴或拖动&降低
您可以使用“oninput”事件,但并非所有浏览器都支持它(ie8+)
限制/过滤表单字段的最佳方法是这样的:

<form id="a" method="post" action="?hey">
<textarea id="t" data-maxlen="10"></textarea>
<input type="submit" value="submit me">
</form>

<script>
var aa = document.getElementById("a");
var tt = document.getElementById("t");
aa.onsubmit=function()
{
    if(tt.value.length > parseInt(tt.getAttribute("data-maxlen")))
    {
        tt.style.backgroundColor="red";
        return false;
    }
    else
    {
        tt.style.backgroundColor="white";
        return true;
    }
};
</script>

但无论如何,您都需要服务器端检查

HTH!

the above code with onkeypress will not work with copy/paste or drag & drop
you can use the "oninput" event but it is not supported in all mayor browsers (ie8+)
the best way to limit/filter a form field is this:

<form id="a" method="post" action="?hey">
<textarea id="t" data-maxlen="10"></textarea>
<input type="submit" value="submit me">
</form>

<script>
var aa = document.getElementById("a");
var tt = document.getElementById("t");
aa.onsubmit=function()
{
    if(tt.value.length > parseInt(tt.getAttribute("data-maxlen")))
    {
        tt.style.backgroundColor="red";
        return false;
    }
    else
    {
        tt.style.backgroundColor="white";
        return true;
    }
};
</script>

but in any case you will need a server-side check

HTH!

吻安 2024-12-15 08:05:19

试试这个:

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

<textarea name="myName" onkeypress="return imposeMaxLength(this, 15);" ><textarea>

或者

<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">
    <textarea name="limitedtextarea" onKeyDown="limitText(this.form.limitedtextarea,this.form.countdown,100);" 
onKeyUp="limitText(this.form.limitedtextarea,this.form.countdown,100);">
    </textarea><br>
    <font size="1">(Maximum characters: 100)<br>
    You have <input readonly type="text" name="countdown" size="3" value="100"> characters left.</font>
</form>

try this:

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

<textarea name="myName" onkeypress="return imposeMaxLength(this, 15);" ><textarea>

or

<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">
    <textarea name="limitedtextarea" onKeyDown="limitText(this.form.limitedtextarea,this.form.countdown,100);" 
onKeyUp="limitText(this.form.limitedtextarea,this.form.countdown,100);">
    </textarea><br>
    <font size="1">(Maximum characters: 100)<br>
    You have <input readonly type="text" name="countdown" size="3" value="100"> characters left.</font>
</form>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文