title: How to read JSON data and compare it with a variable
body: <p>I have two sets of data in a <strong>JSON</strong> file as :</p>
  
  <pre><code>Admins = [{"Admin":"jhonmorris"},{"Admin":"clarkechris"}]
  UIDS = [{"password":"Jhon", "username":"jhonmorris"}, {"password":"Jean", "username":"jeanheadly"}, {"password":"Clarke", "username":"clarkechris"}]
  </code></pre>
  
  <p>I have an <strong>HTML</strong> with a user LogIn Page as : </p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;script type="text/javascript" src="data.json" &gt;&lt;/script&gt;
  &lt;script type="text/javascript" src="checking.js" &gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
  &lt;form name="myForm" method="post"&gt;
  UserName&lt;input type="text" id="uid" value="uid"/&gt;
  UserPassword&lt;input type="password" id="pwd" value="pwd"/&gt;
  &lt;input type="submit" value="Submit" onclick="fnCheck()"/&gt;
  &lt;/form&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I have two types of users, one is an <code>admin</code> (<code>Admins</code>in <strong>JSON</strong>) and the other is a <code>regular user</code> (any other user username apart from <code>Admins</code> in the <strong>JSON</strong>). When the submit button is clicked, I want to take the userID entered in a var <code>x</code> and check if it has the correct password corresponding to it from the <code>UIDS</code> in the <strong>JSON</strong>. Further I want to check if the same username belongs to the <code>Admins</code> set of data in the <strong>JSON</strong>. If yes, I want to redirect him to google. If not, I want to redirect him to yahoo. 
  I am reading the <strong>JSON</strong> data using <strong>JavaScript</strong> as follows : </p>
  
  <pre><code>function fnCheck(){
      alert("in fnCheck()");
      if(checkUP() == true){
          alert("checkUP returned true! UserID and Password are correct");
          if(checkAdmin() == true){
              alert("checkAdmin() returned true! going to admin page now");
              window.location = 'http://www.google.com';
          }
          else if(checkAdmin() == false){
              alert("checkAdmin() returned false! going to userIndex page now");
              window.location = 'http://www.yahoo.com';
          }
      }
      else{
          alert("checkUP() returned false. Creds are Invalid!");
      }
  }
  
  function checkAdmin(){
      alert("in checkAdmin()");
      //var adminJSON = JSON.parse(Admins); 
      //length = adminJSON.length;
      x = document.forms["myForm"]["uid"].value;
  
      for(var key in Admins){
          var admin = Admins[key]
          //alert(""+admin.Admin)
  
          if(admin.Admin == x){
              alert("Admin match found. Returning true!");
              return true;
          }
  
          else{
              alert("Admin match not found. Returning false");
              return false;
          }
      }
  }
  
  function checkUP(){
      alert("hello!!");
      var x = document.forms["myForm"]["uid"].value;
      var y = document.forms["myForm"]["pwd"].value;
      alert("In checkUP, value of x = "+x+" calue of y = "+y);
  
      for(var key in UP){
          var user = UP[key]
  
          //if(user.username == x &amp;&amp; user.password == y){
          if(x.match(user.username) &amp;&amp; x.match(user.password)){
              alert("user.username = "+user.username+"user.password = "+user.password+" User Id and password matched. Returning true now");
              //alert("Matching "+x+ "with "+user.username"+ and "+y+" with user.password"+ y);
              //break;
              return true;
          }
  
          else{
              //alert("User ID and password did not match. Returning false now. user.username = "+user.username+" user.password = "+user.password);
              //break;
              return false;
          }
      }
  }
  </code></pre>
  
  <p>Ive tried putting alerts to find out what is going wrong at all places, but I still cant figure out what is going wrong. Help is much appreciated!</p>