如果两个字段中的密码匹配,我希望显示“密码设置”文本?我究竟做错了什么?关于脚本中包含括号的规则是什么?我可以在同一个脚本标记中拥有两个函数吗?
<p>First name: <input type="text" id="fname" /></p>
<p>Surname: <input type="text" id="sname" /></p>
<p>Date of birth: <input type="text" id="dob" /> (Please enter as dd/mm/yyyy)</p>
<button onclick="user()">Generate Username</button>
<p id="username"></p>
<p id="match"></p>
<p>Please enter a password for your account.</p>
<p>Enter password: <input type="text" id="password1" /></p>
<p>Re-enter password: <input type="text" id="password2"/></p>
<button onclick="password()">Submit Password</button>
<script>
function user() {
document.getElementById("username").innerHTML = document.getElementById("fname").value+document.getElementById("sname").value+document.getElementById("dob").value.slice(-4)
}
function password() {
if document.getElementById("password1").value = document.getElementById("password2").value
document.getElementById("match").innerHTML = " Your password has been set "
}
</script>
最佳答案
我希望这有帮助:)
function password(){
var pass1 = document.getElementById("password1").value;
var pass2 = document.getElementById("password2").value;
if(pass1 == pass2){
alert("password match");
}else{
alert("password do not match");
}
}
<p>Enter password: <input type="password" id="password1" /></p>
<p>Re-enter password: <input type="password" id="password2"/></p>
<button onclick="password()">Submit Password</button>
关于javascript - 如何编写一个程序来确保两个字段中输入的密码匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55812779/