简体   繁体   English

达到最大长度后如何禁用TextBox中的输入

[英]How to disable input in TextBox after it reaches its max length

I have set my textbox maxlength to 20, where I am using validation control to give the error message and it works fine, but actually I want it to display character count the moment user starts entering input in textbox and when it reaches 20 character then user should not be able to type in it. 我已经将我的文本框maxlength设置为20,在这里我使用验证控件给出错误消息,并且可以正常工作,但是实际上我希望它在用户开始在文本框中输入输入时显示字符计数,当它达到20个字符时,用户应该无法输入。 Can this be done, Can anyone shed light on it please: 可以做到这一点吗?

You need to use Javascript for this : 您需要为此使用Javascript:

JS JS

function tocheck(field, count, limit)
{
if (field.value.length > limit)
   field.value = field.value.substring(0, limit);
else
   count.value = limit - field.value.length;
}

Asp.net Asp.net

    <asp:TextBox ID="txtMessage" TextMode="MultiLine"  Width="200px" Rows="3" 
runat="server"  onkeyup="tocheck(txtMessage, this.form.remLen, 20);" onkeydown="tocheck(txtMessage, this.form.remLen, 20);" />

In another textbox you check the count: 在另一个文本框中,检查计数:

<input readonly="readonly" type="text" name="remLen" size="3" maxlength="3" value="160" />

I write this code as simple as possible. 我写的代码尽可能简单。 You need handle some other event such as onPaste as well and write it more efficient. 您还需要处理其他事件,例如onPaste ,并将其写入效率更高。 By the way. 顺便说说。

I assumed: You have jQuery: 我假设:您有jQuery:

<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.min.js"></script>

And these Control: 这些控件:

  <asp:TextBox ID="txtMessage" TextMode="MultiLine" runat="server"></asp:TextBox>
    <asp:Label ID="lblCount" runat="server"></asp:Label>

You need just some JavaScript code like this: 您只需要这样的一些JavaScript代码:

<script>
            var max = 20;
            var lblCount = $("[id$='lblCount']");
            var txtMessage = $("[id$='txtMessage']");

            function checkCount(padding) {
                var currentLength = txtMessage.val().length;
                if (currentLength >= 20) {
                    return false;
                } else {
                    lblCount.text(max - currentLength - padding);
                }
                return true;
            }


            lblCount.text(max);
            txtMessage.keypress(function (e) {
                var key = e.keyCode || e.which;
                if (key != 8 && key != 46) {
                    if (!checkCount(1)) {
                        e.preventDefault();
                    };
                };
            });
            txtMessage.keyup(function (e) {
                var key = e.keyCode || e.which;
                if (key != 8 && key != 46) {
                    e.preventDefault();
                    return;
                }
                checkCount(0);
            });
        </script>

In very start part of JavaScript code I defined max, lblCount an txtMessage that you must be aware of them. 在JavaScript代码的最开始部分,我定义了max,lblCount一个txtMessage,您必须了解它们。

VB.NET VB.NET

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs) Handles TextBox1.KeyPress
    If TextBox1.Text.Length >= 25 Then 
        If e.KeyChar <> ControlChars.Back Then
            e.Handled = True
        End If
    End If
End Sub

'25 is the Limit User Input '25是极限用户输入

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM