gpt4 book ai didi

javascript - 需要密码对话框 Javascript 帮助

转载 作者:行者123 更新时间:2023-11-28 07:27:10 25 4
gpt4 key购买 nike

我需要创建一个密码对话框,告诉您输入密码,然后输入后显示一个警报对话框,提示您输入了密码,并以星号和普通文本显示密码。我有这个位,但如果我的密码包含空格,需要它来阻止我输入密码。例如比尔·盖茨就不应该工作。有人可以帮忙吗?这是我到目前为止所拥有的。

<html>
<head>
<title> Password Alert Box</title>
<script type="text/JavaScript">
//declared variables
var input1 = 0;

input1=prompt("Please enter your Password here","Enter Password Here");//made a prompt box to enter the password
var asterisks = (new Array(input1.length+1).join("*"));//converts the password string into asterisks
window.alert("Valid password "+ asterisks + "\n You entered the password " + input1); //outputs the message valid password, along with the string entered in asterisks, also outputs the password in plain text
</script>
</head>
<body>
</body>
</html>

最佳答案

试试这个代码

function psw() {
var input1 = prompt("Please enter your Password here","Enter Password Here");

if(input1.indexOf(" ") > 0) {
alert('error can not contain spaces');
psw();
return false;
}

var asterisks = (new Array(input1.length+1).join("*"));

alert("Valid password "+ asterisks + "\n You entered the password " + input1);
}

psw();

注意:我使用了与 IE9+ 一起使用的 indexOf

DEMO

关于javascript - 需要密码对话框 Javascript 帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29513902/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com