JavaScript js 仿password功能

來源:互聯網
上載者:User

javascript仿password功能,將使用者輸入的資訊替換成圓點。然後得到使用者輸入的字串;當然還有其他的操作需要處理,這裡就不一一處理;這裡是大致實現的思路和方法。</p><p><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><br /><html><br /> <head><br /> <title>filterStr.html</title></p><p> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><br /> <meta http-equiv="description" content="this is my page"><br /> <meta http-equiv="content-type" content="text/html; charset=UTF-8"><br /> <script type="text/javascript"><br /> String.prototype.replaceAll = function(s1, s2) {<br />var temp = this;<br />while (temp.indexOf(s1) != -1) {<br />temp = temp.replace(s1, s2);<br />}<br /> return temp;<br />}</p><p> function filter(event, o) {<br /> var e = event || window.event;<br /> var currentKey = event.keyCode || event.which || event.charCode;<br /> var temp = o.value;<br /> var filterVal = "";<br /> if (currentKey == 13 || (currentKey > 16 && currentKey < 46) || (currentKey > 112 && currentKey < 123) || currentKey == 144) {<br /> //這裡需要屏蔽掉一些無輸入字元的鍵,如:斷行符號、shift、alt、Home、delete等;暫時就列舉這麼多,其他就不一一列舉;<br /> return;<br /> } else if (currentKey == 8) {<br /> filterVal = o.getAttribute("filterValue") == undefined ? "" : o.getAttribute("filterValue");<br /> filterVal = filterVal.substring(0, temp.length);<br /> o.setAttribute("filterValue", filterVal);<br /> } else {<br /> var startIndex = temp.lastIndexOf('●');<br /> startIndex = startIndex == -1 ? 0 : startIndex + 1;<br /> var val = temp.substring(startIndex, temp.length);<br /> filterVal = o.getAttribute("filterValue") == undefined ? "" : o.getAttribute("filterValue");<br /> filterVal += val;<br /> o.setAttribute("filterValue", filterVal);<br /> var ary = temp.split('');<br /> var result = "";<br /> for (var i = 0; i < ary.length; i++) {<br /> if (ary[i] != "●") {<br /> result = temp.replaceAll(ary[i], '●');<br /> }<br /> }<br /> o.value = result;<br /> }<br /> }</p><p> function getVal() {<br /> alert(document.getElementById("s").getAttribute("filterValue"));<br /> }<br /> </script></p><p> </head></p><p> <body><br /> <input type="text" id="s" onkeyup="filter(event, this)"/><br /> <input type="button" value=" get Value " onclick="getVal()"><br /> </body><br /></html><br />

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.