gpt4 book ai didi

jquery - 使用jquery在密码字段中进行密码屏蔽

转载 作者:行者123 更新时间:2023-12-01 00:52:50 24 4
gpt4 key购买 nike

如何像在 Android 手机中一样进行密码屏蔽,例如当我们输入一个键时,它会显示一个键几秒钟并将其更改为“*”。我尝试了Password masking as in mobiles using js中提到的插件这不是最佳的。还有 jsfiddle http://jsfiddle.net/medopal/X37Wz/无法处理退格键和删除。您能建议任何其他方法来做到这一点吗?只使用jquery,不使用插件。

最佳答案

这里我已经完成了上述查询的完整解决方案。请检查下面给出的演示链接:

演示: http://codebins.com/bin/4ldqp8u

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>

JQuery:

$(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)
}, 500);
});

});

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));
}

}

演示: http://codebins.com/bin/4ldqp8u

关于jquery - 使用jquery在密码字段中进行密码屏蔽,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11822963/

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