SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2011
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript login page array issue

    Im doing a login page for an assessment and allowed to use online sources and open book. Iv made a login page making use of an array which is asked in my assignment but when it runs it accepts any password and username you enter. I need it to accept only what is in the array.

    from what i have done i do not understand as to me it is logical ..

    Any help would be appreciated.

    HTML Code:
    <html>
    
    <script type="text/javascript">
    
    
    
    function userLogin(user1,pass1)
    {
    	var userName = user1.value; 
    	var userPass = pass1.value;
    	var correct = 0;	// set variable of correct so that if its turned ON ( True ) it runs the statement bellow. 
    	var wrong = 1; // set variable of Wrong so that if wrong is switched false then it alerts the user.
    
    	var i = 0
    	var userarray = ["User1", "User2", "User3", "User4", "User5"]; // set usernames and passwords in the array.
    	var passarray = ["Pass1", "Pass2", "Pass3", "Pass4", "Pass5"];
    
    
    	for (var i=0; i < userarray.length; i++)
    		{
    			if (( userName == userarray[i]) && ( userPass == passarray[i])) // if variables userName and userPass are equals to the array then correct = true.
    				{
    					correct = 1;
    				}
    		}	
    
    
    	if (correct = 1) // when correct is true this statement is run. 
    		{
    			window.location="bookingPage.htm" // launches the bookings page.	
    			wrong = 0;
    		}
    
    	else
    		{
    			alert ("Access Denied");
    			correct = 0;
    		}
    }
    
    
    </script>
    
    	
    
    	<body>
    	
    	<center>
    	
        
    	
    <br />
    
    
    <table border="1">
    <tr>
    <td colspan="2">
    <center>
    <h1><b>Login Area</b></h1>
    </center>
    </td>
    </tr>
    
    <tr>
    <td>
    <h1><b>Username</b></h1>
    </td>
    <td><form name="login">
    <input name="user1" type="text"></td>
    </tr>
    
    <tr>
    <td><h1><b>Password</b></h1></td>
    <td><input name="pass1" type="password"></td>
    </tr>
    
    <tr>
    <td>
    <center>
    <input type="button" value="Login" onClick="userLogin(user1,pass1)">
    </center>
    </td>
    
    <td>
    <center><br>
    
    </td>
    </tr>
    </table>
    </center> 
    	
    	
    	</body>
    	</html>

  2. #2
    SitePoint Enthusiast OMGCarlos's Avatar
    Join Date
    Apr 2012
    Location
    Boston, MA
    Posts
    91
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Check out this line:
    Code:
    if (correct = 1) // when correct is true this statement is run. 
    		{
    			window.location="bookingPage.htm" // launches the bookings page.	
    			wrong = 0;
    		}
    
    	else
    		{
    			alert ("Access Denied");
    			correct = 0;
    		}
    In the if statement, you are doing an assignment (correct = 1) instead of a comparison (correct == 1) or (correct === 1). Basically, the program reads it as:
    Code:
    if(1)
    Which is always going to return true no matter what.

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2011
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OMGCarlos i cannot thank you enough!

    one of the 1st things i learnt was comparisons.. such a simple basic error! I feel so embarrassed i never noticed that! >

  4. #4
    SitePoint Enthusiast OMGCarlos's Avatar
    Join Date
    Apr 2012
    Location
    Boston, MA
    Posts
    91
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    No problem, they are hard to catch! I do it every so often too.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •