输入密码时 JavaScript 密码屏蔽

标签 javascript jquery html

我正在寻找 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/

相关文章:

javascript - 当我在路由中使用渲染时 react 返回 Outlet 组件

javascript - Google Chrome 中的开发者工具未显示

html - css 粗体第一个字

javascript - Node.js 要求不按预期工作

javascript - 使用 animate.css 构建一个每 6 秒更改一次 div 的简单代码

JQUERY,将 LI 附加到 UL,然后对该 LI 进行动画处理

javascript - jQuery Sortable 更新数据未序列化

javascript - 滑动切换 [Jquery 函数] 不工作

css - Desktop View/View Desktop Site 的实际作用是什么?

html - 更改悬停/事件按钮