溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時(shí)間:2020-08-26 13:14:51 來源:腳本之家 閱讀:282 作者:weixin_43431702 欄目:web開發(fā)

直接粘貼到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 >職業(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 >
     <button
      type="reset"
      
      "refresh()"
     >
      重置
     </button>
     <button
      type="submit"
      
      "return checkNull()"
     >
      提交
     </button>
    </div>
   </form>
  </div>
 </body>
</html>

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

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI