//删除左右空格
String.prototype.trim = function() {
return this.replace(/(^\s*)|(\s*$)/g, "");
}
String.prototype.lenB = function() {
return this.replace(/[^\x00-\xff]/g, "**").length;
} //一个汉字算2个字节
var maxl = 400 //总长
document.onkeyup = function() {
var s = document.getElementById("comment_deatal").value.lenB();
document.getElementById("charCount").innerHTML = s;
}
function checkTextLength() {
var length = document.forms[0].comment_deatal.value.trim().lenB();
if (length < 1) {
alert("对不起,请输入评论内容!");
return;
} else if (length > 400) {
alert("对不起,您输入的长度超过限制!");
return;
}
document.commentForm.action = "<%=webapp%>/comment.do?method=saveComment";
document.commentForm.submit();
}
实时提示输入字节数
最新推荐文章于 2021-05-31 06:52:35 发布
本文介绍了一种JavaScript中自定义字符串处理方法的方法,包括去除字符串两端空白字符及计算包含汉字在内的字符串长度的功能。通过对原生字符串方法的扩展,可以更方便地进行前端开发中的文本处理任务。
249

被折叠的 条评论
为什么被折叠?



