JS检查限制字符,单词个数

转自: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>

转载于:https://www.cnblogs.com/easy66/archive/2006/12/23/601574.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值