HTML的最大长度< textarea> [英] Max length for HTML <textarea>
问题描述
如何限制可以输入到HTML < textarea>
的最大字符数?我正在寻找一个跨浏览器解决方案。
TEXTAREA
标签没有
INPUT
标签的 MAXLENGTH
属性,至少不是大多数标准浏览器。将可以键入的字符数限制为 TEXTAREA
标记的一种非常简单有效的方法是:
< textarea onKeyPress =return(this.value.length< 50);>< / textarea>
注意: onKeyPress
,将阻止任何按钮按任何按钮 包括退格键。
布尔表达式比较字符长度
,然后将新字符添加到所需的最大长度(在此示例中为50,在此使用自己的值),如果还有空间,则返回true, false
如果没有。从大多数事件返回false将取消默认操作。
所以如果当前长度已经是50(或更多),处理程序返回false,
KeyPress
操作被取消,
软膏中的一滴是可以粘贴到 TEXTAREA
,
不会导致 KeyPress
事件触发,规避此检查。
Internet Explorer 5+包含一个 onPaste
事件,其处理程序可以包含
检查。但是,请注意,您还必须考虑有多少
个字符正在等待剪贴板知道总数是否将
带你超过限制。幸运的是,IE还从窗口对象中包含了一个剪贴板
对象。 1 因此:
< textarea onKeyPress =return(this.value.length< 50 );
onPaste =return((this.value.length +
window.clipboardData.getData('Text')。length)< 50);>< / textarea>
同样, onPaste
事件和 clipboardData
对象只有IE 5+。对于跨浏览器解决方案,您只需使用 OnChange
或 OnBlur
处理程序检查长度,根据需要处理它(以静默方式截断值,通知用户等)。不幸的是,这不会捕获错误,因为它发生,只有当用户尝试离开字段,这不是很友好。
此外,还有另一种方法,包括您可以在您的网页中包含的完成脚本:
http://cf-bill.blogspot.com/2005/05/textarea-maxlength-revisited.html
How to restrict the maximum number of characters that can be entered into an HTML <textarea>
? I'm looking for a cross-browser solution.
The TEXTAREA
tag does not have a MAXLENGTH
attribute the way that an
INPUT
tag does, at least not in most standard browsers. A very simple and effective way to limit the number of characters that can be typed into a TEXTAREA
tag is:
<textarea onKeyPress="return ( this.value.length < 50 );"></textarea>
Note: onKeyPress
, is going to prevent any button press, any button including the backspace key.
This works because the Boolean expression compares the field's length
before the new character is added to the maximum length you want (50 in this example, use your own here), and returns true if there is room for one more, false
if not. Returning false from most events cancels the default action.
So if the current length is already 50 (or more), the handler returns false,
the KeyPress
action is cancelled, and the character is not added.
One fly in the ointment is the possibility of pasting into a TEXTAREA
,
which does not cause the KeyPress
event to fire, circumventing this check.
Internet Explorer 5+ contains an onPaste
event whose handler can contain the
check. However, note that you must also take into account how many
characters are waiting in the clipboard to know if the total is going to
take you over the limit or not. Fortunately, IE also contains a clipboard
object from the window object.1 Thus:
<textarea onKeyPress="return ( this.value.length < 50 );"
onPaste="return (( this.value.length +
window.clipboardData.getData('Text').length) < 50 );"></textarea>
Again, the onPaste
event and clipboardData
object are IE 5+ only. For a cross-browser solution, you will just have to use an OnChange
or OnBlur
handler to check the length, and handle it however you want (truncate the value silently, notify the user, etc.). Unfortunately, this doesn't catch the error as it's happening, only when the user attempts to leave the field, which is not quite as friendly.
Also, there is another way here, including a finished script you could include in your page:
http://cf-bill.blogspot.com/2005/05/textarea-maxlength-revisited.html
这篇关于HTML的最大长度< textarea>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!