如何为多行文本框设置最大长度?

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

问题描述

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

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
how do i pass parameters to aspnet reportviewer(如何将参数传递给aspnet report查看器)
Bind multiple parameters from route and body to a model in ASP.NET Core(在ASP.NET Core中将路由和主体中的多个参数绑定到一个模型)
Custom model binding in AspNet Core WebApi?(AspNet Core WebApi中的自定义模型绑定?)
How to minify in .net core mvc view?(如何在.Net核心MVC视图中缩小?)