JavaScript以防止在n utf 8个字符后写入表单元素 [英] javascript to prevent writing into form elements after n utf 8 characters

查看:145
本文介绍了JavaScript以防止在n utf 8个字符后写入表单元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我的数据库,表格和表格行使用了 utf8_unicode_ci 排序规则。 我的网站语言不常见字符如ş,Ğ,ö,ü,ç,İ



我有服务器端验证&验证规则,但为了访问者的舒适度,我想使用一个javascript代码:

1 - )防止在<$ c之后写入表单输入$ c> n utf8 字符,同时实现实时倒计时。

2 - )我需要轻松地填充javascript函数,因为不同的表单输入在同一个html页面中具有不同的 n 值



我搜索了我的目标,不幸的是我只能从下面给出的SO中找到一个部分的东西(在textarea中使用javascript计算字节数



它计算textarea在UTF8时的字节长度。

  getUTF8Length:function(string){
var utf8length = 0;
for(var n = 0; n var c = string.charCodeAt(n);
if(c <128){
utf8length ++; ((c> 127)&&(c< 2048)){
utf8length = utf8length + 2;
}
}
else {
utf8length = utf8length + 3;
}
}
return utf8length;
}

我也在 http://jsbin.com/fafonimo/3/edit ,这也在下面给出(一些回答者可能想要利用):

HTML

 < ;!DOCTYPE html> 
< html>
< head>
< meta charset =utf-8>
< title> JS Bin< / title>
< / head>
< body>
< form method =postaction =#>
<! - FıstıkçıŞahapöğlenülüküsüçerçöp - >
<! - - 35个字符以上 - >
< label class =c1for =id-input>作者姓名< / label>
< input class =c1type =textid =id-inputname =author>
< input class =c1type =submitvalue =Send>
< / form>
< / body>
< / html>

CSS

  .c1 {width:80%;行高:1em的;填充:1em; margin:1em 10%;} 

这绝对不是我对PHP或MySQL的态度,直接请求解决方案代码抱歉。



关于

解决方案

I在这里找到答案 http://jsfiddle.net/67XDq/7/



问候


HTML

  < tr id =rq17> 
< td class ='qnum'> 17.< / td>
< td class ='qtext'>问题? < i>最多500个字符 - < input style =color:red; font-size:12pt; font-style:italic; readonly type =textid ='q17length'name =q17lengthsize =3maxlength =3value =500> < / i>< br />< textarea onKeyDown =textCounter(this,500);
onKeyUp =textCounter(this,'q17length',500)class =scanwidname =q17id =q17rows =5cols =>< / textarea><< / ; / TD>
< / tr>

JS

  function textCounter(field,cnt,maxlimit){
var cntfield = document.getElementById(cnt)
if(field.value.length> maxlimit)// if too long ...修剪它!
field.value = field.value.substring(0,maxlimit);
//否则,更新'剩下的字符'counter
else
cntfield.value = maxlimit - field.value.length;
}


I use utf8_unicode_ci collation for my database, tables and table rows.

My site language will have uncommon characters like ş, Ğ, ö, ü, ç, İ

I have server side verification & validation rules however for visitor comfort, I want to use a javascript code that:

1-) prevent writing into form input after n utf8 characters while achieving a live countdown.

2-) I need to easily populate the javascript function since different form inputs have different values of n in the same html page

I searched for my aim and unfortunately I only could find a partial thingy from SO given below (Count bytes in textarea using javascript)

it counts how long in bytes a textarea is when 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;
 }

I also created a sample and simple HTML 5 form on http://jsbin.com/fafonimo/3/edit which is given below also (some answerers may want to utilize):

HTML

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>JS Bin</title>
  </head>
  <body>
    <form method="post" action="#">
      <!--Fıstıkçı Şahap öğlen ülüküsü çerçöp-->
      <!--35 characters above-->
      <label class="c1" for="id-input">NAME OF THE AUTHOR</label>
      <input class="c1" type="text" id="id-input" name="author">
      <input class="c1" type="submit" value="Send">
    </form>
  </body>
</html>

CSS

.c1 {width:80%; line-height:1em; padding:1em;margin:1em 10%;}

it's absolutely not my attitude for PHP or MySQL however sorry for requesting the solution code directly.

regards

解决方案

I found the answer here http://jsfiddle.net/67XDq/7/

regards

HTML

<tr id="rq17">
<td class='qnum'>17.</td>
<td class='qtext'>Questions? <i>Maximum of 500 characters - <input style="color:red;font-size:12pt;font-style:italic;" readonly type="text" id='q17length' name="q17length" size="3" maxlength="3" value="500"> characters left</i><br/><textarea onKeyDown="textCounter(this,500);"
onKeyUp="textCounter(this,'q17length' ,500)" class="scanwid" name="q17" id="q17" rows="5" cols=""></textarea></td>
</tr>

JS

function textCounter(field,cnt, maxlimit) {         
    var cntfield = document.getElementById(cnt) 
     if (field.value.length > maxlimit) // if too long...trim it!
        field.value = field.value.substring(0, maxlimit);
        // otherwise, update 'characters left' counter
        else
        cntfield.value = maxlimit - field.value.length;
}

这篇关于JavaScript以防止在n utf 8个字符后写入表单元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆