function Length_Limit()
{
var txtNote; //文本框
var limitCount; //限制的字数
var isbyte; //是否使用字节长度限制(1汉字=2字符)
this.init=function()
{
txtNote=this.txtNote;
limitCount=this.limitCount;
isbyte=this.isbyte;
txtNote.οnkeydοwn=function(){wordsLimit()};
txtNote.οnkeyup=function(){wordsLimit()};
}
function wordsLimit()
{
var noteCount=0;
if(isbyte)
{
noteCount=txtNote.value.replace(/[^\x00-\xff]/g,"xx").length
}
else
{
noteCount=txtNote.value.length
}
if(noteCount>limitCount){
if(isbyte)
{
txtNote.value=txtNote.value.substring(0,txtlength+Math.floor((limitCount-txtByte)/2));
txtByte=txtNote.value.replace(/[^\x00-\xff]/g,"xx").length;
}else
{
txtNote.value=txtNote.value.substring(0,limitCount);
}
}
txtlength=txtNote.value.length; //记录每次输入后的长度
txtByte=txtNote.value.replace(/[^\x00-\xff]/g,"xx").length;
}
}