我得到了客户的要求,说限制textarea - 英文200个字符,UTF8大约是66个汉字。
我想知道如何检查它们是中文字符还是英文字符,并根据最大长度计算它们?
答案 0 :(得分:0)
好吧,所以我找到了一种方法(基于UTF-8),对我来说有点用处。但是当我想到客户端的时候,它仍然是“yucks”,因为他们在检查dom元素时仍然可以编辑它.......
请指教。感谢
代码: 我的textarea和计数框:
<textarea class="message" name="quiz_answer" cols="30" rows="4"></textarea><br />
<span class="countdown"></span>
准备好Jquery文档:
updateCountdown();
$('.message').attr('maxLength','25');
$('.message').keyup(updateCountdown);
updateCountdown function():
var previous = '';
var current = '';
//Count textarea words
function updateCountdown() {
var countMe = $(".message").val();
if(previous==''){
previous = countMe;
current = countMe;
}else{
current = countMe;
}
var escapedStr = encodeURI(countMe);
if (escapedStr.indexOf("%") != -1) {
var count = escapedStr.split("%").length - 1;
if (count == 0) count++ //perverse case; can't happen with real UTF-8
var tmp = escapedStr.length - (count * 3);
count = count + tmp;
} else {
count = escapedStr.length;
}
// 200 is the max message length
var remaining = 200 - count;
$('.countdown').text(remaining + ' characters remaining.');
if(remaining < 0){
$(".message").val(previous);
}else{
previous = current;
}
}