简体   繁体   English

如何在密码字段中显示4位数字

[英]How to display 4 digits in the password field

I'm using normal JS and JSP which contains normal HTML tags. 我正在使用包含普通HTML标记的普通JS和JSP。 I have an input field with the type as PASSWORD which contains the maxlength of 10 digits. 我有一个类型为PASSWORD的输入字段,其中包含10位数字的最大长度。

Now I want to display the last 4 digits of the field values and other digits should be masked. 现在,我想显示字段值的最后4位数字,其他数字应被屏蔽。

I'm not using jQuery and I want to use in normal JS. 我没有使用jQuery,而是想在普通JS中使用。

So can anyone please suggest me any approach on it to achieve. 因此,任何人都可以向我建议实现此目标的任何方法。

Try following these steps. 请尝试按照以下步骤。

  1. Get the password value. 获取密码值。
  2. Get the 2 parts (last 4 characters and the remaining leading characters). 获取2个部分(最后4个字符其余的前导字符)。
  3. Replace the leading characters with ( ASCII-7 character). ASCII-7字符)替换前导字符。
  4. Generate new password to show (masked + 4 visible characters). 生成新密码以显示(带掩码的+ 4个可见字符)。
  5. Set the password value. 设置密码值。

Check out this fiddle . 看看这个小提琴

Here is the snippet. 这是代码段。

 var passField = document.getElementById('pass'); passField.type = "text"; var passValue = passField.value; var passLength = passValue.length; var masked = passValue.substring(0, passLength - 4); masked = masked.replace(/./g, '•'); //The character is ASCII-7 (Press Alt+7 to type) var text = passValue.substring(passLength - 4); var newPass = masked + text; passField.value = newPass; 
 <input type='password' id='pass' value="ThisIsPassword" /> 

CSS 的CSS

#wrapper {
    position: relative;
}

#wrapper > input {
    font-family: monospace;
    text-align: right;
}

#wrapper > [type=password]::-ms-reveal{
    display: none;
}

#passwordMasked {
    width: 10em;
    border: solid 1px black;
    border-right: none;
}

#wrapper > #passwordUnmasked {
    border: solid 1px black;
    border-left: none;
    width: 3em;
    text-align: left;
}

#password { 
    position: absolute;
    left: 0;
    opacity: 0;
    width: 13em;
}

HTML 的HTML

<div id="wrapper">
    <input type="password" onkeyup="updateunmasked()" id="passwordMasked" /><input type="text" id="passwordUnmasked" readonly /><input type="password" onkeyup="updateunmasked()" id="password" />
</div>

Javascript Java脚本

function updateunmasked() {
    var p = document.getElementById("password").value;
    document.getElementById("passwordUnmasked").value = ('    ' + p.substring(Math.max(p.length - 4, 0))).substring(Math.min(p.length, 4));
    document.getElementById("passwordMasked").value = p.substring(4);
}

JSBin - https://jsbin.com/wijifupuco/1/edit?html,css,js,output JSBin- https: //jsbin.com/wijifupuco/1/edit ? html,css,js,输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM