HTML DOM 輸入文字 maxLength 屬性


HTML DOM 輸入文字 maxLength 屬性用於設定或返回輸入文字欄位的 maxLength 屬性。maxLength 屬性指定可以在文字欄位中輸入的最大字元數。

語法

以下為 − 的語法

設定 maxLength 屬性 −

textObject.maxLength = integer

其中,整型指定可在文字欄位中輸入的最大字元數。

示例

讓我們看一個 maxLength 屬性的示例 −

動態演示

<!DOCTYPE html>
<html>
<body>
<h1>Input Text maxLength Property</h1>
USERNAME: <input type="text" id="USR" maxLength="5">
<p>Increase the maximum number of characters to be entered for the above field by clicking below button</p>
<button onclick="changeMax()">CHANGE LENGTH</button>
<p id="Sample"></p>
<script>
   function changeMax() {
      document.getElementById("USR").maxLength = "15";
      document.getElementById("Sample").innerHTML = "Maximum number of characters are now increased to 15";
   }
</script>
</body>
</html>

輸出

這將產生以下輸出。由於我們已將 maxLength 設為 5,因此只允許輸入 5 個字元 −

點選 CHANGE LENGTH 按鈕 −

更新於:2021 年 2 月 19 日

145 次瀏覽

開啟 職業生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.