0

I have been working on an assignment and I have been asked to create a login page by using HTML and javascripts by implementing hashmaps, I have implemented it by using variables. cuser and cpass holds the correct username and the password and "t1" and "t2" are the textfields where the user types in the username and password.

var cuser="admin";
var cpass="pass";    
var user=document.getElementById("t1").value;    
var pass=document.getElementById("t2").value;

and I have used an if condition to validate the username and password

     if(user == cuser && pass == cpass)
    {
        alert("You are logged in as : " +cuser);
    }

I have to implement this using hashmaps, can someone please help me with this how can I create a hashtable and validate the user and password using the key and the value.

1

2 Answers 2

1

Try in this manner,

var credentials = {cuser:"admin",cpass:"pass"};
var user=document.getElementById("t1").value;    
var pass=document.getElementById("t2").value;

if(user == credentials['cuser'] && pass == credentials['cpass']) {
        alert("You are logged in as : " +cuser);
}

Or you can use dot notation instead of bracket notation like credentials.cuser

Sign up to request clarification or add additional context in comments.

Comments

0

I would have done it like this:

var users= {
      "[email protected]": {pass:"one"}, 
      "[email protected]": {pass:"two"}
};
//You can store more user related information like firstname, lastname etc. in the above JSON and accessing the data will be very easy for particular user e.g. users["[email protected]"].pass

var user=document.getElementById("t1").value;    
var pass=document.getElementById("t2").value;

if(users[user] && pass == users[user].pass) {
    alert("You are logged in as : " + user);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.