Limit the number of characters of a textbox [duplicate]

心不动则不痛 提交于 2019-12-23 02:36:28

问题


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>


来源:https://stackoverflow.com/questions/11444943/limit-the-number-of-characters-of-a-textbox

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!