js form表單input框限制20個字符,10個漢字代碼實例
更新時間:2019年04月12日 17:05:30 作者:weixin_43431702
這篇文章主要介紹了js form表單input框限制20個字符,10個漢字,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
直接粘貼到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> // 這是第一種最尋常的方法 //定義一個函數(shù) function checkField(value, byteLength, attribute) { // value是input框的值,byteLength是需求限制的字符,attribute是input的id名 var newvalue = value.replace(/[^\x00-\xff]/g, "**"); var length = newvalue.length; //當填寫的字節(jié)數(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); //校驗點是否為“×” //if 基點是×; 判斷在基點內有×為偶數(shù)還是奇數(shù) if (count % 2 == 0) { //當為偶數(shù)時 size = count / 2 + (byteLength * 1 - count); limitvalue = value.substr(0, size); } else { //當為奇數(shù)時 size = (count - 1) / 2 + (byteLength * 1 - count); limitvalue = value.substr(0, size); } alert( "最大輸入" + byteLength + "個字節(jié)(相當于" + byteLength / 2 + "個漢字)!" ); 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個字符,10個漢字詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!
相關文章
js使用文檔就緒函數(shù)動態(tài)改變頁面內容示例【innerHTML、innerText】
這篇文章主要介紹了js使用文檔就緒函數(shù)動態(tài)改變頁面內容,結合實例形式分析了JavaScript使用innerHTML、innerText函數(shù)動態(tài)操作頁面元素相關使用技巧,需要的朋友可以參考下2019-11-11JSON.stringify轉換JSON時日期時間不準確的解決方法
這篇文章主要介紹了JSON.stringify轉換JSON時日期時間不準確的解決方法,即JSON數(shù)據(jù)中包含日期對象時,在轉換時會轉換成國際時間,而不是中國的時區(qū),需要的朋友可以參考下2014-08-08