Password masking in password field using jquery

后端 未结 3 1569
不思量自难忘°
不思量自难忘° 2021-01-14 09:24

How to do password masking as in Android mobile like when we type in a key it shows a key for few seconds and change it to \"*\". I tried the plugin mentioned in Password m

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-14 10:03

    based on keyur at codebins.com code above, this great little answer here https://stackoverflow.com/a/4843500/1056285 and also https://stackoverflow.com/a/1431109/1056285 (for making the backspace actually delete without bugs) i adapted the solution to take into account backspace too!

    $(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);
        });
    
        $("#txtpwd").keydown(function(e) {
            if (e.keyCode == 8) {
                setTimeout(function() {
                    maskPassword(e)
                }, 1);
            }
        });
    
    });
    
    function generateStars(n) {
        var stars = '';
        for (var i = 0; i < n; i++) {
            stars += '*';
        }
        return stars;
    }
    
    function maskPassword(e) {
    
        var text = $('#txthidden').val();
        var stars = $('#txthidden').val().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().length;
    
        if (unicode != 9 && unicode != 13 && unicode != 37 && unicode != 40 && unicode != 37 && unicode != 39) {
            //If NOT  OR  Then...
            if (unicode != 8 && unicode != 46) {
                text = text + String.fromCharCode(unicode);
                stars += 1;
            }
            //If Press  Or  Then...
            else if ((unicode == 8 || unicode == 46) && stars != PwdLength) {
                stars -= 1;
                text = text.substr(0, curPos) + text.substr(curPos + 1);
            }
            //Set New String on both input fields
            $('#txthidden').val(text);
            $('#txtpwd').val(generateStars(stars));
        }
    
    }
    

    You can have a look below: http://codebins.com/bin/4ldqp8u/38

提交回复
热议问题