jQuery 获取密码字段的掩码值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6778011/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting the masked value of a password field?
提问by Industrial
I've got a normal password field from which I would like to "get" the masked value - yep, that ugly **********
obfuscated value.
我有一个普通的密码字段,我想从中“获取”掩码值 - 是的,那个丑陋的**********
混淆值。
HTML:
HTML:
<input type="password" name="password" value="" id="password"></input>
JS/JQ DOM:
JS/JQ DOM:
$("#password").val(); // Password in cleartext
回答by Nicola Peluchetti
If you mean that you want a string that contains as much *
as the number of characters you inserted in a password field you could do:
如果您的意思是您想要一个包含与*
您在密码字段中插入的字符数一样多的字符串,您可以执行以下操作:
var password = $("#password").val();
password = password.replace(/./g, '*');
回答by Lightness Races in Orbit
Get a string repeat functionon the go, then use this:
随时随地获取字符串重复函数,然后使用:
repeat('*', $('#myfield').val().length);
or (depending on the implementation you go with):
或(取决于您使用的实现):
'*'.repeat($('#myfield').val().length);
My personal suggestion:
我的个人建议:
function repeat(s, n) {
return new Array(isNaN(n) ? 1 : ++n).join(s);
}
var password = "lolcakes";
console.log(repeat('*', password.length));
// ^ Output: ********
回答by James McCormack
Do a regex replace on $("#myfield").val();
that replaces all characters with *
?
是否使用正则表达式$("#myfield").val();
替换所有字符*
?
alert($("#myfield").val().replace(/./g, '*'));