HTML DOM Input Password maxLength 屬性
HTML DOM 輸入密碼 maxlength 屬性用於設定或返回輸入密碼欄位的 maxlength 屬性。maxLength 屬性指定在密碼欄位中鍵入的最大字元數。
語法
以下是 − 的語法
設定 maxLength 屬性 −
passwordObject.maxLength = integer
在此,integer 指定密碼欄位中可鍵入的最大字元數。
示例
讓我們看一個 maxLength 表單屬性的示例 −
<!DOCTYPE html> <html> <body> <h1>Input Password maxLength Property</h1> Password: <input type="password" id="PASS" 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("PASS").maxLength = "15"; document.getElementById("Sample").innerHTML = "Maximum number of characters are now increased to 15"; } </script> </body> </html>
輸出
這將產生以下輸出 −
點選更改長度並輸入更多字元 −
在上面的示例中
我們首先建立了一個輸入密碼欄位,其 id 為“PASS”,並將其 maxLength 屬性值設定為 5。這意味著該欄位中只能輸入 5 個字元。
Password: <input type="password" id="PASS" maxLength=”5”>
然後建立了一個按鈕更改長度,當用戶單擊該按鈕時,它將執行 changeMax() 方法 −
<button type="button" onclick="changeMax()">CHANGE LENGTH</button>
changeMax() 方法使用 getElementById() 方法獲取型別為密碼的輸入欄位,並將其 maxLength 屬性設定為 15。然後,我們使用其 innerHTML 屬性在帶 id 為“Sample”的段落中顯示訊息,以顯示此更改 −
function changeMax() { document.getElementById("PASS").maxLength = "15"; document.getElementById("Sample").innerHTML = "Maximum number of characters are now increased to 15"; }
廣告