转自:http://bbs.blueidea.com/thread-2704372-1-1.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>檢查輸入長度、中文及英文字數</title>
<script language="javascript">
<!--
var maxWord=10; //中文及英文最大字數
var maxCharacter=50; //字元字數
//檢查輸入中文及英文字數
function changeTextWord(objElement) {
var sng=false;
if(changeText(objElement)){sng=true;}else{return false;}
var oTextCountWord = $("txtCountWord_"+objElement.id);
var tmp="";
tmp=objElement.value.replace(/[^\u0000-\u00ff]/g," a ");
tmp=tmp.replace(/\b([a-z0-9]+)\b/gi,"a");
tmp=tmp.replace(/\s/g,"");
if(tmp.length>maxWord){
sub(objElement);
if(event.keyCode==8){sng=true;}else{sng=false;};
}else{
sng=true;
}
oTextCountWord.innerHTML = "<font color=#0000FF>"+ tmp.length +"</font>";
objElement.style.background = '#FFFFFF' ;
return sng;
}
//檢查輸入長度
function changeText(objElement) {
var oTextCount = $("txtCount_"+objElement.id);
//iCount = objElement.value.length;
iCount = objElement.value.replace(/[^\u0000-\u00ff]/g,"aa").length;
if(iCount>maxCharacter){
sub(objElement);
if(event.keyCode!=8){return false;};
}
oTextCount.innerHTML = "<font color=#0000FF>"+ iCount+"</font>";
return true;
}
function resizeEditor(change,obj) {
var newheight = parseInt($(obj).style.height,10) + change;
if(newheight >= 20) {
$(obj).style.height = newheight + 'px';
}
}
function $(obj){
return document.getElementById(obj);
}
function sub(obj){
obj.value=obj.value.substring(0,obj.value.length-1);
changeTextWord(obj);
}
-->
</script>
</head>
<body>
<form>
目前字數總共:<span id="txtCountWord_taAbObjective"><font color="#0000FF">0</font></span> 字(Word),<span id="txtCount_taAbObjective"><font color="#0000FF">0</font></span> 字元(Character)<br>
<textarea id="taAbObjective" rows="6" cols="60" wrap="virtual" οnkeydοwn="return changeTextWord(this);" οnkeyup="return changeTextWord(this);" οnblur="changeTextWord(this)" οnpaste="return changeTextWord(this);" οndrag="javascript:return false;" style="border:1px solid #000000; height:100px;"></textarea>
<div class="editor_textexpand"><img src="images/common/bb_contract.gif" width="11" height="21" title="收縮編輯框" alt="收縮編輯框" οnclick="resizeEditor(-20,'taAbObjective')" /><img src="images/common/bb_expand.gif" width="11" height="21" title="擴展編輯框" alt="擴展編輯框" οnclick="resizeEditor(20,'taAbObjective')" /></div>
目前字數總共:<span id="txtCountWord_taAbObjective2"><font color="#0000FF">0</font></span> 字(Word),<span id="txtCount_taAbObjective2"><font color="#0000FF">0</font></span> 字元(Character)<br>
<textarea id="taAbObjective2" rows="6" cols="60" wrap="virtual" οnkeydοwn="return changeTextWord(this);" οnkeyup="return changeTextWord(this);" οnblur="changeTextWord(this)" οnpaste="return changeTextWord(this);" οndrag="javascript:return false;" style="border:1px solid #000000; height:100px;"></textarea>
<div class="editor_textexpand"><img src="images/common/bb_contract.gif" width="11" height="21" title="收縮編輯框" alt="收縮編輯框" οnclick="resizeEditor(-20,'taAbObjective2')" /><img src="images/common/bb_expand.gif" width="11" height="21" title="擴展編輯框" alt="擴展編輯框" οnclick="resizeEditor(20,'taAbObjective2')" /></div>
</form>
</body>
</html>