文本框字符长度的限制!
<script type="text/javascript">
function textListener(obj,lengthLimit)
{
var byteLength = 0;
var strInput = document.getElementById(obj).value;
for(var i=0;i<strInput.length;i++)
{
var ch = strInput.charAt(i);
if(isNormalChar(ch))
byteLength += 1;
else
byteLength += 2;
}
if(byteLength<lengthLimit)
{
document.getElementById(obj).maxLength = lengthLimit;
}
if(byteLength>=lengthLimit)
{
var str = document.getElementById(obj).value;
var byteLength2 = 0;
var temp = "";
for(var i=0;i<str.length;i++)
{
var ch = str.charAt(i);
if(isNormalChar(ch))
byteLength2 += 1;
else
byteLength2 += 2;
if(byteLength2>lengthLimit)
{
break;
}
temp = temp+ch;
}
document.getElementById(obj).maxLength = temp.length;
document.getElementById(obj).value=temp;
}
}
function isNormalChar(ch)
{
if(ch.length>1){
return false;
}
if(ch == ""){
return true;
}
var pattern = /^([\\uFF66-\\uFF9F]|[\\u0000-\\u00FF])*$/gi;
if (pattern.test(ch)){
return true;
}else{
return false;
}
}
</script>
function textListener(obj,lengthLimit)
{
var byteLength = 0;
var strInput = document.getElementById(obj).value;
for(var i=0;i<strInput.length;i++)
{
var ch = strInput.charAt(i);
if(isNormalChar(ch))
byteLength += 1;
else
byteLength += 2;
}
if(byteLength<lengthLimit)
{
document.getElementById(obj).maxLength = lengthLimit;
}
if(byteLength>=lengthLimit)
{
var str = document.getElementById(obj).value;
var byteLength2 = 0;
var temp = "";
for(var i=0;i<str.length;i++)
{
var ch = str.charAt(i);
if(isNormalChar(ch))
byteLength2 += 1;
else
byteLength2 += 2;
if(byteLength2>lengthLimit)
{
break;
}
temp = temp+ch;
}
document.getElementById(obj).maxLength = temp.length;
document.getElementById(obj).value=temp;
}
}
function isNormalChar(ch)
{
if(ch.length>1){
return false;
}
if(ch == ""){
return true;
}
var pattern = /^([\\uFF66-\\uFF9F]|[\\u0000-\\u00FF])*$/gi;
if (pattern.test(ch)){
return true;
}else{
return false;
}
}
</script>