[英]How to compare passwords in Javascript
I have a registration page and I want to compare two passwords (input fields) to be equal before writing it to a websql database.我有一个注册页面,我想在将它写入 websql 数据库之前比较两个密码(输入字段)是否相等。 I cannot seem to get it to work.
我似乎无法让它工作。 Any ideas?
有任何想法吗?
function addTodo() {
var todo = document.getElementById("todo");
var todo2 = document.getElementById("todo2");
if(todo != todo2) {
alert("Yours passwords do not match");
} else {
curatio.webdb.addTodo(todo.value);
todo.value = "";
alert("Your Registration was successfull");
setTimeout(function () {
window.location.href = "login.html";
}, 1000);
}
}
<div data-role="fieldcontain" >
<label for="todo">
Password
</label>
<input name="" id="todo" placeholder="" value="" type="password" required>
</div>
<div data-role="fieldcontain" >
<label for="todo2">
Retype your Password
</label>
<input name="" id="todo2" placeholder="" value="" type="password" required>
</div>
You're comparing the elements instead of their values.您正在比较元素而不是它们的值。
var todo = document.getElementById("todo");
var todo2 = document.getElementById("todo2");
if(todo != todo2) { // Oops
todo
and todo2
are 2 different <input>
elements. todo
和todo2
是 2 个不同的<input>
元素。
Try using .value
:尝试使用
.value
:
if(todo.value !== todo2.value) {
You're comparing the actual elements, which will always be true
(because they are both TextFields).您正在比较实际元素,这将始终为
true
(因为它们都是 TextField)。 Compair their values, like so:比较它们的值,如下所示:
var todo = document.getElementById("todo").value;
var todo2 = document.getElementById("todo2").value;
Either this or change要么这个要么改变
if(todo != todo2)
to到
if(todo.value != todo2.value)
另一种方法是Object.is(password, confirm_password)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.