Loading ...

JavaScript: Character limit on multiline textbox | CodeAsp.Net

JavaScript: Character limit on multiline textbox

 /5
0 (0votes)

While working with ASP.NET multiline textbox I realized that MaxLength property was not working, it seems it was bypassed by ASP.NET engine. I decided to write a small JavaScript piece of code in order to apply character limit on the textarea. Below is the code for it:

 $(function () { 
var summaryControl = $("#Summary");
var maxLength = 100;
summaryControl.on('keyup', function (e) {
var el = this;
switch (e.keyCode) {
case 37: // left
return true;
case 38: // up
return true;
case 39: // right
return true;
case 40: // down
return true;
case 8: // backspace
return true;
case 46: // delete
return true;
case 27: // escape
el.value = '';
return true;
}
var isExceeded = (el.value.length > maxLength);
if (isExceeded) {
el.value = el.value.substring(0, maxLength);
}
return !isExceeded;
});
});

Live demo: http://jsfiddle.net/raghav_khunger/6kt6t/

 

Comments (no comments yet)

Top Posts