Javascript防止在n utf8字符之后写入表单元素
javascript to prevent writing into form elements after n utf 8 characters
我使用utf8_unicode_ci
排序对我的数据库,表和表行。
我的网站语言将有不常见的字符,如::,Ğ, ö, ü, ç, İ
我有服务器端验证&验证规则,但是为了访问者的舒适,我想使用一个javascript代码:
1-)防止在n
utf8
字符之后写入表单输入,同时实现实时倒计时。
2-)我需要轻松地填充javascript函数,因为不同的表单输入在同一html页面中具有不同的n
值
我搜索了我的目标,不幸的是,我只能从下面给出的SO找到部分内容(使用javascript在textarea中计数字节)
在UTF8时计算文本区域的字节长度。
getUTF8Length: function(string) {
var utf8length = 0;
for (var n = 0; n < string.length; n++) {
var c = string.charCodeAt(n);
if (c < 128) {
utf8length++;
}
else if((c > 127) && (c < 2048)) {
utf8length = utf8length+2;
}
else {
utf8length = utf8length+3;
}
}
return utf8length;
}
我还在http://jsbin.com/fafonimo/3/edit上创建了一个示例和简单的HTML 5表单,下面也给出了(一些答案可能想要利用):