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.

Teemu
  • 22,918
  • 7
  • 53
  • 106
shashi kumar
  • 43
  • 1
  • 4
  • possibily duplicate http://stackoverflow.com/questions/456932/hash-table-in-javascript – brk Feb 07 '16 at 07:41

2 Answers2

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

Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130
0

I would have done it like this:

var users= {
      "someone1@example.com": {pass:"one"}, 
      "someone2@example.com": {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["someone1@example.com"].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);
}
saurav
  • 972
  • 11
  • 24