如何为多行文本框设置最大长度? [英] How to set maxlength for multiline TextBox?

查看:28
本文介绍了如何为多行文本框设置最大长度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 MultiLine TextBox(生成 TextArea)时,设置 MaxLength 属性无效.最好的解决方法是什么?我想用最少的丑陋的 javascript 等获得基本的、预期的功能.只是防止用户输入超过最大数量的字符.

When using a MultiLine TextBox (which generates a TextArea) setting the MaxLength property has no effect. What is the best workaround? I'd like to get basic, intended functionality with minimum of ugly javascript etc. Just prevent user from entering more than max number of characters.

推荐答案

如果你想让用户知道他是否超过了他写的字符数,你可以使用一个附加到 keypress 事件的 javascript 函数.此函数将测试输入的长度,如果达到最大长度,则取消字符渲染.

If you want to let the user know if he exceeded the amount of characters as he writes, you could use a javascript function attached to keypress event. This function would test the length of the input and cancel the character rendering if the maxlenght was reached.

另一种选择是使用 RegularExpressionValidator 控件来验证提交时的输入.

Another option is to use RegularExpressionValidator control to validate the input on submit.

在我看来,第一种选择要好得多.

In my opinion, the first option is much more better.

我没有添加任何代码,因为谷歌充满了各种口味的示例,这是一项非常常见的任务.

I'm not adding any code since google is full of examples for all tastes, this is a very common task.

这里您有一个可能有帮助的示例搜索.

Here you have a sample search that might help.

这篇关于如何为多行文本框设置最大长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆