HTML 中的字符限制

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/113376/
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-28 22:20:43  来源:igfitidea点击:

Character Limit in HTML

htmlcharacter-limit

提问by Iwasakabukiman

How do you impose a character limit on a text input in HTML?

如何对 HTML 中的文本输入施加字符限制?

回答by e-satis

There are 2 main solutions:

主要有2种解决方案:

The pure HTML one:

纯 HTML 之一:

<input type="text" id="Textbox" name="Textbox" maxlength="10" />

The JavaScript one (attach it to a onKey Event):

JavaScript 之一(将其附加到 onKey 事件):

function limitText(limitField, limitNum) {
    if (limitField.value.length > limitNum) {
        limitField.value = limitField.value.substring(0, limitNum);
    } 
}

But anyway, there is no good solution. You can not adapt to every client's bad HTML implementation, it's an impossible fight to win. That's why it's far better to check it on the server side, with a PHP / Python / whatever script.

但无论如何,也没有好的解决办法。您无法适应每个客户的糟糕 HTML 实现,这是一场不可能获胜的战斗。这就是为什么最好在服务器端检查它,使用 PHP / Python / 任何脚本。

回答by cruizer

there's a maxlength attribute

有一个 maxlength 属性

<input type="text" name="textboxname" maxlength="100" />

回答by Devin Jeanpierre

In addition to the above, I would like to point out that client-side validation (HTML code, javascript, etc.) is never enough. Also check the length server-side, or just don't check at all (if it's not so important that people can be allowed to get around it, then it's not important enough to really warrant any steps to prevent that, either).

除了上述之外,我想指出客户端验证(HTML 代码、javascript 等)是远远不够的。还要检查服务器端的长度,或者根本不检查(如果它不是那么重要以至于人们可以绕过它,那么它也不足以真正保证采取任何措施来防止这种情况发生)。

Also, fellows, he (or she) said HTML, not XHTML. ;)

另外,伙计们,他(或她)说的是 HTML,而不是 XHTML。;)

回答by nickf

use the "maxlength" attribute as others have said.

使用其他人所说的“maxlength”属性。

if you need to put a max character length on a text AREA, you need to turn to Javascript. Take a look here: How to impose maxlength on textArea in HTML using JavaScript

如果您需要在文本区域上放置最大字符长度,则需要使用 Javascript。看看这里:如何使用 JavaScript 在 HTML 中的 textArea 上施加 maxlength

回答by pilsetnieks

For the <input> element there's the maxlength attribute:

对于 <input> 元素,有 maxlength 属性:

<input type="text" id="Textbox" name="Textbox" maxlength="10" />

(by the way, the type is "text", not "textbox" as others are writing), however, you have to use javascript with <textarea>s. Either way the length should be checked on the server anyway.

(顺便说一句,类型是“文本”,而不是其他人写的“文本框”),但是,您必须使用带有 <textarea> 的 javascript。无论哪种方式,都应该在服务器上检查长度。

回答by shekh danishuesn

you can set maxlength with jquery which is very fast

您可以使用非常快的 jquery 设置 maxlength

jQuery(document).ready(function($){ //fire on DOM ready
 setformfieldsize(jQuery('#comment'), 50, 'charsremain')
})