密碼文字框可以視為一種特殊文字框,屬性與普通文字框幾乎相同,唯一差別是輸入的內容會被遮成圓點或星號。這樣設計的目的很單純:避免旁人看見你的密碼。最典型的例子就是平時用的帳號登入畫面。
語法:
<input type="password"/>
舉例 1:
<!DOCTYPE html> <html xmlns="https://www.w3.org/1999/xhtml"> <head> <title></title> </head> <body> <form name="form1" method="post" action="index.html"> 賬號:<input type="text"/><br/> 密碼:<input type="password"/> </form> </body> </html>
屬性組合與普通文字框完全相同:
| 屬性 | 說明 |
|---|---|
| value | 定義文字框的預設值,也就是框內的初始文字 |
| size | 定義文字框的長度,以字元為單位 |
| maxlength | 限制最多可輸入的字元數 |
屬性之間沒有先後順序。input 還有一個 name 屬性,在 XHTML 中已被摒棄,掌握上面三個就夠。
<input type="password" value="預設文字" size="文字框長度" maxlength="最多輸入字元數"/>
舉例:
<!DOCTYPE html> <html xmlns="https://www.w3.org/1999/xhtml"> <head> <title></title> </head> <body> <form name="form1" method="post" action="index.html"> 賬號:<input type="text" size="15" maxlength="10"/><br/> 密碼:<input type="password" size="15" maxlength="10"/> </form> </body> </html>
外觀跟舉例 1 差不多,但文字框長度與可輸入字元數已經被限制住了。
password 只做到「讓旁邊的人看不到你打了什麼」,它並不能真正保護資料。要確保資料傳輸安全,得在瀏覽器與伺服器之間建立加密連線(例如 HTTPS),那已經不是前端能獨力解決的問題了。