<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>js判断输入字符串长度(汉字算两个字符,字母数字算一个)</title>
<scripttype="text/javascript">
// 获取字符串长度(汉字算两个字符,字母数字算一个)
function getByteLen(val) {
var len = 0;
for (var i = 0; i < val.length; i++) {
var a= val.charAt(i);
if (a.match(/[^\x00-\xff]/ig) != null) {
len += 2;
}else {
len += 1;
}
}
return len;
}
// 只要键盘一抬起就验证编辑框中的文字长度,最大字符长度可以根据需要设定
function checkLength(obj) {
var maxChars= 120;//最多字符数
var curr= maxChars- getByteLen(obj.value);
if (curr > 0) {
document.getElementById("checklen").innerHTML = curr.toString();
} else {
document.getElementById("checklen").innerHTML = '0';
document.getElementById("subject").readOnly = true;
}
}
</script>
</head>
<body>
<div class="pbt cl">
<textarea id="subject" maxlength="80" οnkeyup="checkLength(this)" accesskey="1" tabindex="11"></textarea>
<span id="subjectchk">还可输入<strong id="checklen" style="color: #FF0000">80</strong>个字符</span>
<span id="postNameRule"style="display: none"></span>
</div>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>js判断输入字符串长度(汉字算两个字符,字母数字算一个)</title>
<scripttype="text/javascript">
// 获取字符串长度(汉字算两个字符,字母数字算一个)
function getByteLen(val) {
var len = 0;
for (var i = 0; i < val.length; i++) {
var a= val.charAt(i);
if (a.match(/[^\x00-\xff]/ig) != null) {
len += 2;
}else {
len += 1;
}
}
return len;
}
// 只要键盘一抬起就验证编辑框中的文字长度,最大字符长度可以根据需要设定
function checkLength(obj) {
var maxChars= 120;//最多字符数
var curr= maxChars- getByteLen(obj.value);
if (curr > 0) {
document.getElementById("checklen").innerHTML = curr.toString();
} else {
document.getElementById("checklen").innerHTML = '0';
document.getElementById("subject").readOnly = true;
}
}
</script>
</head>
<body>
<div class="pbt cl">
<textarea id="subject" maxlength="80" οnkeyup="checkLength(this)" accesskey="1" tabindex="11"></textarea>
<span id="subjectchk">还可输入<strong id="checklen" style="color: #FF0000">80</strong>个字符</span>
<span id="postNameRule"style="display: none"></span>
</div>
</body>
</html>