TextArea限制输入长度

时间:2023-12-12 18:48:08

cs文件中添加文本框属性this.TextBox.Attributes.Add("MaxLength", "200");

  $(document).ready(function() {
$("textarea").each(function() {
var maxLength = $(this).attr("MaxLength");
if (maxLength) {
$(this).bind("keyup", "", function(e) {
var le = $(this).val().len();
if (le > maxLength) {
$(this).val($(this).val().substring(0,maxLength))
}
});
}
});
});
//返回字符串的实际长度, 一个汉字算2个长度
String.prototype.len = function() {
return this.replace(/[^\x00-\xff]/g, "**").length;
}