Wednesday 19 August 2009

Supporting maxlength for textarea using jQuery


/*
* Plugin for jQuery JavaScript Library
* http://dev-lessons-learned.blogspot.com/
*
* Copyright (c) 2009 Sandesh Tathare
* Licensed for free use.
*
* Date: 2009-08-20 10:15
* Revision: 001
*/
$(document).ready(function (){
maxlength();
});

function maxlength(){
$("textarea[maxlength]").each(function(){
var maxlength = ($(this).attr("maxlength"));
$(this).keypress(function(event){
if(maxlength < ($(this).val().length + 1)){
event.preventDefault();
}
});

$(this).keyup(function(event){
if(maxlength < ($(this).val().length)){
$(this).val($(this).val().substring(0,maxlength));
}
});
});
}

No comments:

Post a Comment