How To Restrict Input In MultiLine TextBoxes

MultiLine TextBoxes are rendered as a TEXTAREA to the client. There is no maxlength property on a TEXTAREA.

When you set the the TextMode to MultiLine in an ASP.NET TextBox the MaxLength property is ignored. To get around this obstacle use JavaScript:

function CharCountRestriction

Param: text (text to evaluate)
Param: long (length of text to restrict upon)

Restricts input of characters into multiline textboxes

Implementation: Add the following to your Textbox: onKeyUp="CharCountRestriction(this,200)" onChange="CharCountRestriction(this,200)" 

<script language="javascript">
        function CharCountRestriction(text, long) {
            var maxlength = new Number(long);
            if (text.value.length > maxlength) {
                text.value = text.value.substring(0, maxlength);
                alert(" Only " + long + " chars");
 <asp:TextBox runat="server" ID="tbJobName" TextMode="MultiLine"
            onKeyUp="CharCountRestriction(this,200)" onChange="CharCountRestriction(this,200)"></asp:TextBox>

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s