获取密码字段的掩码值?

时间:2011-07-21 14:49:17

标签: jquery html input passwords

我有一个正常的密码字段,我希望从中获取屏蔽值 - 是的,那个丑陋**********模糊的值。

HTML:

<input type="password" name="password" value="" id="password"></input>

JS / JQ DOM:

$("#password").val(); // Password in cleartext

3 个答案:

答案 0 :(得分:10)

如果您想要一个字符串,其中包含您在密码字段中插入的字符数*,那么您可以这样做:

var password = $("#password").val();
password  = password.replace(/./g, '*');

答案 1 :(得分:3)

随时随地获取a string repeat function,然后使用:

repeat('*', $('#myfield').val().length);

或(取决于您使用的实施方式):

'*'.repeat($('#myfield').val().length);

我的个人建议:

function repeat(s, n) {
    return new Array(isNaN(n) ? 1 : ++n).join(s);
}

var password = "lolcakes";
console.log(repeat('*', password.length));
// ^ Output: ********

Live demo.

答案 2 :(得分:1)

在用$("#myfield").val();取代所有字符的*上替换正则表达式吗?

alert($("#myfield").val().replace(/./g, '*'));