javascript - Json Login with multiple user credentials -
i trying login form using json, below code:
<html> <head> <title> login form</title> <script> function validate(){ var username = document.getelementbyid("username").value; var password = document.getelementbyid("password").value; var userlist = [{"username":"asdf", "password":"123"}, {"username":"zxc", "password":"123"}] var jobj = json.parse(userlist); (var i=0; < jobj.length; i++) { if ( username == jobj.username && password == jobj.password) { alert ("login successfully"); } else{ alert("invalid username , password"); } } } </script> </head> <body> <div class="container"> <div class="main"> <h2>login form</h2> <form id="form_id" method="post" name="myform"> <label>user name :</label> <input type="text" placeholder="enter username" name="username" id="username"/> <label>password :</label> <input type="password" placeholder="enter password" name="password" id="password"/> <input type="button" value="login" id="submit" onclick="validate()"/> </form> </body> </html>
i unable achieve this, when enter given credentials nothing appearing on screen, have add more functionality achieve?
you don't need :
var jobj = json.parse(userlist);
just parse array :
function validate() { var username = document.getelementbyid("username").value; var password = document.getelementbyid("password").value; var userlist = [{"username":"asdf", "password":"123"}, {"username":"zxc", "password":"123"}]; var logged = false; (var i=0; < userlist.length; i++) { if ( username == userlist[i].username && password == userlist[i].password) logged=true; } if(logged) alert ("login successfully"); else alert("invalid username , password"); }
hope helps.
Comments
Post a Comment