Alavi
Alavi

Reputation: 708

Multi line Text box max length

i want to have <asp:textbox> and set it to multi line but when i set to multi line maxlength property not work. How to control max length of multi line asp:textbox by jquery?

Upvotes: 2

Views: 358

Answers (2)

Mahmoud Amini Arjmand
Mahmoud Amini Arjmand

Reputation: 79

Use a regular expression validator instead. This will work on the client side

<asp:RegularExpressionValidator runat="server" ID="valInput"
    ControlToValidate="txtInput"
    ValidationExpression="^[\s\S]{0,100}$"
    ErrorMessage="Please enter a maximum of 100 characters"
    Display="Dynamic">*</asp:RegularExpressionValidator>

Upvotes: 2

Morteza Jangjoo
Morteza Jangjoo

Reputation: 1790

try it

<script>

        function checkTextAreaMaxLength(textBox, e, length) {

            var mLen = textBox["MaxLength"];
            if (null == mLen)
                mLen = length;

            var maxLength = parseInt(mLen);
            if (!checkSpecialKeys(e)) {
                if (textBox.value.length > maxLength - 1) {
                    if (window.event)//IE
                        e.returnValue = false;
                    else//Firefox
                        e.preventDefault();
                }
            }
        }
        function checkSpecialKeys(e) {
            if (e.keyCode != 8 && e.keyCode != 46 && e.keyCode != 37 && e.keyCode != 38 && e.keyCode != 39 && e.keyCode != 40)
                return false;
            else
                return true;
        }

    </script>

in HTML

<asp:TextBox runat="server" ID="TxtComment" onkeyDown="checkTextAreaMaxLength(this,event,'150');" TextMode="MultiLine" />

Upvotes: 1

Related Questions