Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Basic Login Validation

    Hi. Firstly I must point out, although I know this probably isnt the best way to create a login system. Its just an example for some of my school work.

    Basically, I have a very basic "bare bones" login form.

    What I want to be able to do, is to create an IF statement that will allow only 1 pair of values for the username and password.

    eg. username = john
    password = smith

    now, I want the if statement to run, and look for the values entered in the username and password fields. if the value for username is correct, and the password is correct, i want another page to open, however if any other values are entered, i want an alert message to be thrown. "please enter correct login details".

    My problem with this, Is i do not know how to write an if statement that will except one value, and reject all others.

    Iv included a printscreen of my coding and I warn you, it may be very wrong, but Im confused as to how to make it work.

    Heres the image of my code: http://i39.tinypic.com/e8sb9j.png

    Thanks for your time.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,316
    Thanks
    203
    Thanked 2,566 Times in 2,544 Posts
    accept != except

    Code:
    var uname = document.formname.username.value;
    var pword = document.formname.password.value;
    
    if (uname.toLowercase() == "john" && pword.toLowerCase() == "smith") {
    window.location.href = "anotherPage.html";
    }
    else {
    alert ("Please enter correct login details");
    }
    I expect that you realise that this is hopelessly insecure as anyone can see the correct username and password simply with View Source. Also the identity of "anotherPage.html".

    Quizmaster: A soliloquy is performed by how many people?
    Contestant: Four
    Last edited by Philip M; 01-09-2012 at 08:34 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi. thanks for your reply. sorry i didn't reply sooner, only yesterday I had to rush out late.

    Anyway, I amended the code, and believe its correct, however an error keeps being thrown.

    Script Error: Line 20, Char 1: Unable to get value of property "uName". Object is null or undefined.

    Its probably just a little something I am doing wrong. Again: here's the amended code.

    http://i41.tinypic.com/2j5lvzl.png

    Many thanks again.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,316
    Thanks
    203
    Thanked 2,566 Times in 2,544 Posts
    How can the code be correct if it throws an error?

    Javascript is case-sensitive. uname != uName

    But in fact the error is that you have placed the definitions of username and password outside the function. So the script tries to get the values on page load, at which point they are undefined.

    It is no use posting an image of your code. Show the code (using the code tags) in the usual manner, as text. Then people can run it and expose the errors.
    Last edited by Philip M; 01-10-2012 at 01:10 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    OK. I apologise, but please go easy on me. I'm new to coding and am struggling to understand certain concepts at the moment.

    So I am anticipating that I am still doing it wrong, however I have placed the variables inside the function and I still an error about the username variable being null or undefined.

    sorry about this, I have tried different things, i just cannot seem to get it to work.

    Here's the code:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >

    <head>
    <title>NewDocument</title>
    <meta name="Author" content="" />
    <meta name="Keywords" content="" />
    <meta name="Description" content="" />
    <link href="" rel="stylesheet" type="text/css"/>
    <style type="text/css"></style>

    <script type="text/javascript">

    function checkall(loginChk){

    var uname = document.loginChk.username.value;
    var pword = document.loginChk.password.value;

    if (uname.toLowercase() == "john" && pword.toLowerCase() == "smith") {
    window.location.href = "success.html";
    }
    else {
    alert ("Please enter correct login details");
    }
    }

    </script>

    </head>

    <body>

    <form method="post" onClick=" return checkall (this);">

    <h1>Please Login:</h1>
    Username: &nbsp;<input type="text" name="username" size="20"> <br />
    Password: &nbsp;<input type="password" name="password" size="20"> <br />
    <br />
    <input type="submit" name="submit" value="Login" action="loginChk();"/>

    </form>

    </body>
    </html>

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,316
    Thanks
    203
    Thanked 2,566 Times in 2,544 Posts
    You have not assigned the name loginChk to your form. You are not actually submitting a form to a server, so a submit button is not appropriate. The action of a form is the URL to which it submits - not a script.

    Don't forget that Javascript is case-sensitive, so .toLowercase() fails.
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    
    <head>
    <title>NewDocument</title>
    <meta name="Author" content="" />
    <meta name="Keywords" content="" />
    <meta name="Description" content="" />
    <link href="" rel="stylesheet" type="text/css"/>
    <style type="text/css"></style>
    
    <script type="text/javascript">
    
    function checkall(){ 
    
    var uname = document.loginChk.username.value;
    var pword = document.loginChk.password.value;
    
    if (uname.toLowerCase() == "john" && pword.toLowerCase() == "smith") {
    window.location.href= "http://www.google.com";  // for testing
    //window.location.href = "success.html";
    }
    else {
    alert ("Please enter correct login details");
    document.loginChk.username.value = "";  // clear the fields
    document.loginChk.password.value = "";
    
    }
    }
    
    </script>
    
    </head>
    
    <body>
    
    <form name = "loginChk">
    
    <h1>Please Login:</h1>
    Username: &nbsp;<input type="text" name="username" size="20"> <br />
    Password: &nbsp;<input type="password" name="password" size="20"> <br />
    <br />
    <input type="button" value="Login" onclick = "checkall()"/>
    
    </form>
    
    </body>
    </html>
    I am happy to help you with your homework provided that you learn from it.

    BTW, when posting here please help us to help you by following the posting guidelines and wrapping your code in CODE tags. This means use the octothorpe or # button on the toolbar. You can (and should) edit your previous post.
    Last edited by Philip M; 01-10-2012 at 04:52 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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