亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

js form表單input框限制20個(gè)字符,10個(gè)漢字代碼實(shí)例

 更新時(shí)間:2019年04月12日 17:05:30   作者:weixin_43431702  
這篇文章主要介紹了js form表單input框限制20個(gè)字符,10個(gè)漢字,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

直接粘貼到html文件便可看到效果

<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
  <script>
   // 這是第一種最尋常的方法
   //定義一個(gè)函數(shù)
   function checkField(value, byteLength, attribute) {
    // value是input框的值,byteLength是需求限制的字符,attribute是input的id名
    var newvalue = value.replace(/[^\x00-\xff]/g, "**");
    var length = newvalue.length;
    //當(dāng)填寫的字節(jié)數(shù)小于設(shè)置的字節(jié)數(shù)
    if (length * 1 <= byteLength * 1) {
     return;
    }
    var limitDate = newvalue.substr(0, byteLength);
    var count = 0;
    var limitvalue = "";
    for (var i = 0; i < limitDate.length; i++) {
     var flat = limitDate.substr(i, 1);
     if (flat == "*") {
      count++;
     }
    }
    var size = 0;
    var istar = newvalue.substr(byteLength * 1 - 1, 1); //校驗(yàn)點(diǎn)是否為“×”
    //if 基點(diǎn)是×; 判斷在基點(diǎn)內(nèi)有×為偶數(shù)還是奇數(shù)
    if (count % 2 == 0) {
     //當(dāng)為偶數(shù)時(shí)
     size = count / 2 + (byteLength * 1 - count);
     limitvalue = value.substr(0, size);
    } else {
     //當(dāng)為奇數(shù)時(shí)
     size = (count - 1) / 2 + (byteLength * 1 - count);
     limitvalue = value.substr(0, size);
    }
    alert(
     "最大輸入" +
      byteLength +
      "個(gè)字節(jié)(相當(dāng)于" +
      byteLength / 2 +
      "個(gè)漢字)!"
    );
    document.getElementById(attribute).value = limitvalue;
    return;
   }
  </script>
 </head>
 <body>
  <div class="wrap">
   <form>
    <table class="list-style">
     <tr>
      <td style="width:15%;text-align:right;">職業(yè):</td>
      <td>
       <input id="profession" type="text" "checkField(this.value,20,'profession')" class="textBox length-middle" name="profession"/>
       <input id="interest" type="text" "checkField(this.value,100,'interest')" class="textBox length-middle" name="interest"/>
       <input id="address" "checkField(this.value,40,'address')" type="text" class="textBox length-middle" name="address" />
      </td>
     </tr>
    </table>

    <div style="margin:20px 0 0 300px">
     <button
      type="reset"
      style="width:60px;height:30px;margin-right:40px"
      "refresh()"
     >
      重置
     </button>
     <button
      type="submit"
      style="width:60px;height:30px"
      "return checkNull()"
     >
      提交
     </button>
    </div>
   </form>
  </div>
 </body>
</html>

以上所述是小編給大家介紹的js form表單input框限制20個(gè)字符,10個(gè)漢字詳解整合,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論