The for loop i = 0 checks the password only 0th index I want to check for all index what need to be included.
function validlogin() {
var user = document.getElementById('user').value;
var psw = document.getElementById('psw').value;
var items = JSON.parse(localStorage.getItem('login'));
for (let i = 0; i < items.length; i ) {
if (items[i].username == user && items[i].password == psw)
{
alert("welcome");
}
}
}
CodePudding user response:
You can use .find
to discover the user with the matching username and then validate their password. It looks like you're managing logins on the client-side, definitely don't do this for a production application.
Assuming the login
localStorage object looks like [{username: "Among Us", password: "Red is sus"}]
, here's how you'd do it:
function validlogin() {
const user = document.getElementById('user').value;
const psw = document.getElementById('psw').value;
const items = JSON.parse(localStorage.getItem('login'));
const userData = items.find(({username}) => username === user);
if (!userData) {
alert('No user found');
return;
}
if (userData.password !== psw) {
alert('Incorrect password');
return;
}
alert(`Logged in as ${userData.username}`);
}
CodePudding user response:
You can try something using this logic:
function validlogin() {
var user = document.getElementById('user').value;
var psw = document.getElementById('psw').value;
var items = JSON.parse(localStorage.getItem('login'));
for (let i = 0; i < items.length; i ) {
if (items.username.join('') == user && items.password.join('') == psw)
{
alert("welcome");
}
}
}