Limit the number of characters of a textbox [dupli

2019-06-01 09:15发布

问题:

Possible Duplicate:
Specifying maxlength for multiline textbox
How to set maxlength for multiline TextBox?

I want to limit the number of characters of a text box.

For this i used

<asp:textbox id="txtAdmissionDate" runat="server" MaxLength="5"> </asp:textbox>

It's working fine. But when i add TextMode="MultiLine" attribute to it, the maxlenth is not working.

Please Help

回答1:

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.

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.

or you can get more info from Here also



回答2:

Call the following javascript function on "onchange" event of textbox:

function CheckTextLength(text, long) {
    var maxlength = new Number(long); // Change number to your max length.
    if (text.value.length > maxlength) {
        text.value = text.value.substring(0, maxlength);
        alert(" Only " + long + " characters allowed");
    }
}

 <asp:TextBox runat="server" ID="txtAdmissionDate" Width="450px" TextMode="MultiLine"
            onKeyUp="CheckTextLength(this,5)" onChange="CheckTextLength(this,5)"></asp:TextBox>