所以基本上我将用户在注册表中输入的详细信息存储到本地HTML存储中......我已经检查过详细信息是否存储在本地存储中,但是,当我尝试使用这些信息登录时(用户名)和登录表单中的密码....它不起作用。那么我怎样才能成功登录登录表单?换句话说,我如何能够获得以寄存器形式(存储在本地存储中)输入的用户名和密码的数据,并使用它来与登录表单中的用户输入进行比较以验证登录过程?这是文件:
答案 0 :(得分:3)
您必须将它们与用户进行比较并传递,而不是用户名和密码:
var entry = localStorage.getItem("entry");
console.log("username: " + entry.user + "password: " + entry.pas);
if(username.value == entry.user && password.value == entry.pass) {
alert('You have successfully logged in.');
}
在浏览器中按下CTRL + SHIFT + I,如果它未打开,请选择" Console"
答案 1 :(得分:1)
这些行不起作用:
var storedUserName = localStorage.getItem('UserName');
var storedPassWord = localStorage.getItem('PassWord');
var storedEmailAddress = localStorage.getItem('EmailAddress');
savedata()
不会将每个字段保存到自己的项目中,它只保存entry
个对象作为整体。所以你需要检索并解析它。
var entryJSON = localStorage.getItem('entry');
if (!entryJSON) {
alert("Nothing stored!");
return;
}
var entry = JSON.parse(entryJSON);
var storedUserName = entry.user;
var storedPassWord = entry.pass;
var storedEmailAddress = entry.email;
要搜索allEntries
,您需要使用循环:
function validlogin(event) {
var username = document.getElementById('username').value;
var password = document.getElementById('password').value;
var entriesJSON = localStorage.getItem('allEntries');
if (!entriesJSON) {
alert("Nothing stored!");
event.preventDefault();
return;
}
var allEntries = JSON.parse(entriesJSON);
for (var i = 0; i < allEntries.length; i++) {
var entry = allEntries[i];
var storedUserName = entry.user;
var storedPassWord = entry.pass;
var storedEmailAddress = entry.email;
if (username == storedUserName && password == storedPassWord) {
alert("Successfully logged in!");
return;
}
alert('Invalid Username or Password! Please try again.');
event.preventDefault();
window.location="Login.html";
}