Loading ...

How to validate max length in multiline textbox using jquery

 /5
0 (0votes)

I tried to fix the maximum length in multiline textbox by using the property MaxLength. But it didn't work in case of multiline textbox. In this blog i will explain how to get rid of this problem by using jquery.

$(function() {
$('#ctl00_ContentPlaceHolder1_txtTest').keyup(function() {
limitChars('ctl00_ContentPlaceHolder1_txtTest', 100, 'counter');
})
});

function limitChars(textid, limit, infodiv) {
var text = $('#' + textid).val();
var textlength = text.length;
if (textlength > limit) {
$('#' + infodiv).html('You cannot write more then ' + limit + ' characters!');
$('#' + textid).val(text.substr(0, limit));
return false;
}
else {
$('#' + infodiv).html('You have ' + (limit - textlength) + ' characters left.');
return true;
}
}


<asp:TextBox  id="txtTest" runat="server" TextMode="MultiLine"></asp:TextBox >
<div id="counter">Max 100 characters.</div>

Comments (1)

   
Aaron
does it take care of copy pasting using keyboard and mouse as well....?
9/24/2009
 · 
by

Top Posts