Monday, March 26, 2012

asp dot net multiline textbox maxlength not working

in asp.net the maxlength attribute does not work, if the textbox is multiline.
<asp:TextBox ID="txtEvent" runat="server" MaxLength="500" TextMode="MultiLine"
                        Width="783px" Height="64px"></asp:TextBox>
if its not multiline the maxlength works properly...
this is a known issue and every body shouts...
so the work around is nothing other than the big Javascript.
so do as follows...

<asp:TextBox ID="txtEvent" runat="server" MaxLength="500" TextMode="MultiLine"
                        Width="783px" Height="64px" onKeyUp="Count(this,500);" onChange="Count(this,500);"></asp:TextBox>


The Javascript is

//check the length of the textbox(multiline)
        function Count(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(" Maximum " + long + " characters allowed.");
            }
        }

hope this helps...

No comments:

Post a Comment