限制輸入中文字的字數與英文字的字數

来源:互联网 发布:签名字体设计软件 编辑:程序博客网 时间:2024/04/29 15:36
<script>
function check(){
var newstr="";str=checkthis.checktext.value,max=str.length,addascii=0;addbig5=0,flag=true;
 for(var i=0;i<max && flag<40;i++,flag=addascii+addbig5){var codes=str.charCodeAt(i);
  if(codes>=65 && codes<=122){addascii++;}else{addbig5+=2;}newstr+=str.charAt(i);}
 checkthis.checktext.value=newstr;transfer(newstr);}
function transfer(arg)
{alert("其餘超過字串已切除,送出資料為/n/n"+arg);}
</script>

===HTML===
<form name="checkthis">
<input type="text" name="checktext" size="40">
<input type="button" onclick="check()" value="欄位控制檢查"><sup>(僅對中英文字檢查,勿填入其他字元)</sup>
</form>