Tuesday, 26 November 2013

Limiting the characters in Multi Line / Rich Text using jquery

<script language="javascript" src="/SiteAssets/jquery-1.7.1.min.js" type="text/javascript"></script>
 //site assets is the library where jquery is stored
<script type="text/javascript">  
var MaxLength = 90;
var mytxetArea = "Description"; //text area display name here

$(document).ready(function () {

$("textarea[title='" + mytxetArea + "']").keyup(function (e) {

 if ($(this).val().length > MaxLength) {
$(this).val($(this).val().substring(0, MaxLength));

}

});

$(
"textarea[title='" + mytxetArea + "']").keypress(function (e) {

if ($(this).val().length > MaxLength) {

e.preventDefault();

}

});

$(
"textarea[title='" + mytxetArea + "']").blur(function (e) {

if ($(this).val().length > MaxLength) {

$(this).val($(this).val().substring(0, MaxLength));

}

});

});

</script>