日期:2014-05-16  浏览次数:20308 次

JS检测输入字符个数,区分汉字和英文
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head>     <title>CheckInputLength</title>     <script language="javascript" type="text/javascript">...         function getStringUTFLength(str) ...{             var value = str.replace(/[^\\x00-\\xff]/g," ");             return value.length;         }         function leftUTFString(str,len) ...{             if(getStringUTFLength(str)<=len)                 return str;             var value = str.substring(0,len);             while(getStringUTFLength(value)>len) ...{                 value = value.substring(0,value.length-1);             }             return value;         }         function count() ...{             var value = document.getElementById("licenseother").value;             value = value.replace(/[\\u4e00-\\u9fa5]/g," ");             if(value.length>=20) ...{                 try...{                     with(window.event) ...{                         cancelBubble = true;                         keyCode=0;                         returnValue = false;                     }                 }catch(e)...{}                 document.getElementById("licenseother").value = leftUTFString(document.getElementById("licenseother").value,20);             }             document.getElementById("result").value = 20-getStringUTFLength(document.getElementById("licenseother").value);         }             </script> </head> <body> <table width="100%"> <tr><td> 本输入框限制输入20个字符(汉字计算为2个字符:) </td></tr> <tr><td> <textarea cols=80 rows=3 wrap="virtual" id="licenseother" onkeypress="count()" onkeyup="count()" onblur="count();" onChange="count();"></textarea> </td></tr> <tr><td> 剩余字符数:<input type="text" size="3" id="result" value="20"&g