gpt4 book ai didi

输入密码时 JavaScript 密码屏蔽

转载 作者:行者123 更新时间:2023-11-28 18:52:41 25 4
gpt4 key购买 nike

我正在寻找 JavaScript 函数来屏蔽输入密码并在其他文本框中显示密码。

这是我在互联网上找到的 JavaScript 代码

$(function() {
//Extends JQuery Methods to get current cursor postion in input text.
//GET CURSOR POSITION
jQuery.fn.getCursorPosition = function() {
if (this.lengh == 0) return -1;
return $(this).getSelectionStart();
}

jQuery.fn.getSelectionStart = function() {
if (this.lengh == 0) return -1;
input = this[0];

var pos = input.value.length;

if (input.createTextRange) {
var r = document.selection.createRange().duplicate();
r.moveEnd('character', input.value.length);
if (r.text == '') pos = input.value.length;
pos = input.value.lastIndexOf(r.text);
} else if (typeof(input.selectionStart) != "undefined") pos = input.selectionStart;

return pos;
}

//Bind Key Press event with password field
$("#txtpwd").keypress(function(e) {
setTimeout(function() {
maskPassword(e)
}, 100);
});

});

function generateStars(n) {
var stars = '';
for (var i = 0; i < n; i++) {
stars += '*';
}
return stars;
}

function maskPassword(e) {

var text = $('#txthidden').val().trim();
var stars = $('#txthidden').val().trim().length;
var unicode = e.keyCode ? e.keyCode : e.charCode;
$("#keycode").html(unicode);

//Get Current Cursor Position on Password Textbox
var curPos = $("#txtpwd").getCursorPosition();
var PwdLength = $("#txtpwd").val().trim().length;

if (unicode != 9 && unicode != 13 && unicode != 37 && unicode != 40 && unicode != 37 && unicode != 39) {
//If NOT <Back Space> OR <DEL> Then...
if (unicode != 8 && unicode != 46) {
text = text + String.fromCharCode(unicode);
stars += 1;
}
//If Press <Back Space> Or <DEL> Then...
else if ((unicode == 8 || unicode == 46) && stars != PwdLength) {
stars -= 1;
text = text.replace(text.charAt(curPos), "");
}
//Set New String on both input fields
$('#txthidden').val(text);
$('#txtpwd').val(generateStars(stars));
}
}

html 给出为

<div id="panel">
<input type="text" id="txtpwd" name="txtpwd" size="15"/>
<input type="text" id="txthidden" name="txthidden" size="15"/>
<div>
KeyCode Pressed:
<span id="keycode">
</span>
</div>
</div>

现在,当我在 txtpwd 字段中输入内容时,它会转换为星号并在 txthidden 文本框中打印文本值。但是当我从 txtpwd 文本框中删除时,它不会删除 txthidden 中的文本。演示可以在这里找到 http://codebins.com/bin/4ldqp8u

请告诉我如何修复它。提前致谢

最佳答案

i was looking for the javascript function to mask the input password along with showing the password in other textbox

在 HTML5 中,默认情况下有一个 input field for passwords ,以便在您输入输入内容时将其屏蔽。

<div id="panel">
<input type="text" id="txtpwd" name="txtpwd" size="15"/>
<input type="password" id="txthidden" name="txthidden" size="15"/>
<div>

关于输入密码时 JavaScript 密码屏蔽,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34224590/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com