<form action="liuyan.php" method="post" name="frm" onsubmit="check();">
输入您的留言:<br/><textarea name="liuyan" cols="50" rows="8" warp="soft" id="liuyan" onkeyup="checkLength(this);" ></textarea>
<div class="right"><font size="2">文字最大长度: 250. 剩余字符: <span id="chLeft">250</span>.</font></div>
</form>这是html页面的代码
下面是js的代码<script>
function checkLength(which){
var maxChars = 250;
if (which.value.length > maxChars)
which.value = which.value.substring(0,maxChars);
var curr = maxChars - which.value.length;
document.getElementById("chLeft").innerHTML = curr.toString();
}
function check() {
if (document.frm.liuyan.value=="") {
alert("留言内容不能为空!");
return false;
}
}
</script>可以动态的计算文本框的输入情况,但是汉字这里还是按照一个字符计算的,请教高手,如果输入汉字的话怎么算2个字符
解决方案 »
- JSP页面使用OCX时,OCX中的Msgbox反应非常慢
- 请大家来看看我自编的yy网页,做到这步该怎么做
- html里面的js传递路径问题
- showmodelessdialog传值问题
- 网页用mideaplay播放时一执行javascript就播止
- 网页整体大小是采用100%的,<iframe style="width:100%;height:100%;scrolling:auto">会有滚动条,怎么能设置为自动的?
- JS 正则,帮忙替换下
- 支付宝在IE下打开有个绿色的边框,是通过什么技术实现的啊
- 有什么好的javascript编辑器吗?
- javascript 动态增加表单域
- js 里面能有这样的写法吗?我很无奈
- JQ写层的显示和隐藏的问题
for(var i=0;i<str.length;i++) {
var temp = str.charCodeAt(i);
if(temp >= 0 && temp <= 254){ }判断字符串的ASCII值是否在中文字符的区间吧,在的话算2个,不在算1个
function checkLength(which){
var maxChars = 250;
var re = /[ox0-oxff]/gi;
var oLength = which.value.replace(re,"").length;
if (which.value.length+oLength > maxChars){
which.value = which.value.substring(0,maxChars-oLength);
}
var curr = maxChars - which.value.length-oLength;
document.getElementById("chLeft").innerHTML = curr.toString();
}
另外最好把onkeyup改成onpropertychange
function checkLength(which){
var maxChars = 250;
if (textbyte(which.value) > maxChars)
which.value = gettextBybyte(which.value,maxChars);
var curr = maxChars - textbyte(which.value);
document.getElementById("chLeft").innerHTML = curr.toString();
}
function check() {
if (document.frm.liuyan.value=="") {
alert("留言内容不能为空!");
return false;
}
}
function textbyte(str){
var count=0;
for(i=0;i<str.length;i++)(escape(str.charAt(i)).length< 4)?count++:count+=2;
return count;
}
function gettextBybyte(str,len){
var count=0;
var rtn=""
for(i=0;count<len;i++)(escape(str.charAt(i)).length< 4)?count++:count+=2;
return str.substring(0,count>50?i-1:i);
}</script><form action="liuyan.php" method="post" name="frm" onsubmit="check();">
输入您的留言:<br/><textarea name="liuyan" cols="50" rows="8" warp="soft" id="liuyan" onkeyup="checkLength(this);" ></textarea>
<div class="right"><font size="2">文字最大长度: 250. 剩余字符: <span id="chLeft">250</span>.</font></div>
</form>
var maxChars = 250;
var re = /[ox0-oxff]/gi;//匹配单字节字符
var oLength = which.value.replace(re,"").length;//删除单字节字符,留下的全是双字节字符,实际字节长度 = 字符串长度 + 双字节个数
if (which.value.length+oLength > maxChars){
which.value = which.value.substring(0,maxChars-oLength);
}
var curr = maxChars - which.value.length-oLength;
document.getElementById("chLeft").innerHTML = curr.toString();
}
这个不能用啊,语法规则是什么?可不可以透露下
补充一下:
for(var i=0;i<str.length;i++) {
var temp = str.charCodeAt(i);
if(temp >= 0 && temp <= 254){
alert("是英文字符");
}else{
alert("是中文字符");
}
}遍历字符串就行了,
貌似字符长了效率不行的
用onpropertychange则可以捕获值改变的任何情况,但是onpropertychange不被FF所支持
在FF中需要使用oninput
onpropertychange="checkLength(this);
function checkLength(which,p){
var maxChars = 250;
if(p)which.value=which.value + ""+clipboardData.getData("text");
if (textbyte(which.value) > maxChars)
which.value = gettextBybyte(which.value,maxChars);
var curr = maxChars - textbyte(which.value);
document.getElementById("chLeft").innerHTML = curr.toString();
return false;
}
function check() {
if (document.frm.liuyan.value=="") {
alert("留言内容不能为空!");
return false;
}
}
function textbyte(str){
var count=0;
for(i=0;i<str.length;i++)(escape(str.charAt(i)).length< 4)?count++:count+=2;
return count;
}
function gettextBybyte(str,len){
var count=0;
var rtn=""
for(i=0;count<len;i++)(escape(str.charAt(i)).length< 4)?count++:count+=2;
return str.substring(0,count>50?i-1:i);
}</script><form action="liuyan.php" method="post" name="frm" onsubmit="check();">
输入您的留言:<br/><textarea name="liuyan" cols="50" rows="8" warp="soft" id="liuyan" onpaste="return checkLength(this,'paste');" onkeyup="checkLength(this);"></textarea>
<div class="right"><font size="2">文字最大长度: 250. 剩余字符: <span id="chLeft">250</span>.</font></div>
</form>
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>test</title>
<script>
window.onload = function(){
if(navigator.userAgent.indexOf("MSIE")>=0) {
document.getElementById("liuyan").attachEvent('onpropertychange',function(o){
checkLength(document.getElementById("liuyan"));
});
}else{
document.getElementById("liuyan").addEventListener('input',function(o){
checkLength(document.getElementById("liuyan"));
},false);
}
};function checkLength(which){
var maxChars = 250;
var re = /[ox0-oxff]/gi;
var oLength = which.value.replace(re,"").length;
if (which.value.length+oLength > maxChars){
which.value = which.value.substring(0,maxChars-oLength);
}
var curr = maxChars - which.value.length-oLength;
document.getElementById("chLeft").innerHTML = curr.toString();
}
</script>
</head><body>
<form action="liuyan.php" method="post" name="frm" onsubmit="check();">
输入您的留言:<br/><textarea name="liuyan" cols="50" rows="8" warp="soft" id="liuyan"></textarea>
<div class="right"><font size="2">文字最大长度: 250. 剩余字符: <span id="chLeft">250</span>.</font></div>
</form>
</body></html>