Creating a login in javascript - array comparison - Stack Overflow

I have a login box using a simple javascript login paring usernames and passwords, before you all start

I have a login box using a simple javascript login paring usernames and passwords, before you all start I know about the security issues in using javascript for authentication. Here is the code

function validate() {
    var un = document.getElementById("usern").value;
    var pw = document.getElementById("pword").value;
    var valid = false;

    var unArray = ["markwalt", "jongossy", "lisacain", "jenndemp"];
    var pwArray = ["mark1234", "flomaygo", "lisa1234", "jenny1234"];
    var fnArray = ["Mark Walters", "Jonathan Goss", "Lisa Cain", "Jenny Dempsey"];

    for (var i=0; i <unArray.length; i++) {
        if ((un == unArray[i]) && (pw == pwArray[i])) {
            valid = true;
            break;
        }
    }

    if (valid) {
        alert ("Login was successful");
        document.getElementById("mandatory1").value = un;
    }
    else {
        alert("Invalid Username and/or Password! Please try again. You will not be able to submit this form without a successful login")
        document.getElementById("pword").value = "";
        document.getElementById("usern").value = "";
        document.getElementById("usern").focus();
    }
}

At the moment if the login is successful I'm posting the username to a hidden field which is then being used by a piece of a software. How do I associate the names in fnArray with the other correct username & password so that I can then grab associated full name and post that to the hidden field "mandator1" instead?

I have a login box using a simple javascript login paring usernames and passwords, before you all start I know about the security issues in using javascript for authentication. Here is the code

function validate() {
    var un = document.getElementById("usern").value;
    var pw = document.getElementById("pword").value;
    var valid = false;

    var unArray = ["markwalt", "jongossy", "lisacain", "jenndemp"];
    var pwArray = ["mark1234", "flomaygo", "lisa1234", "jenny1234"];
    var fnArray = ["Mark Walters", "Jonathan Goss", "Lisa Cain", "Jenny Dempsey"];

    for (var i=0; i <unArray.length; i++) {
        if ((un == unArray[i]) && (pw == pwArray[i])) {
            valid = true;
            break;
        }
    }

    if (valid) {
        alert ("Login was successful");
        document.getElementById("mandatory1").value = un;
    }
    else {
        alert("Invalid Username and/or Password! Please try again. You will not be able to submit this form without a successful login")
        document.getElementById("pword").value = "";
        document.getElementById("usern").value = "";
        document.getElementById("usern").focus();
    }
}

At the moment if the login is successful I'm posting the username to a hidden field which is then being used by a piece of a software. How do I associate the names in fnArray with the other correct username & password so that I can then grab associated full name and post that to the hidden field "mandator1" instead?

Share Improve this question edited Nov 19, 2013 at 7:01 Kara 6,22616 gold badges53 silver badges58 bronze badges asked Jun 7, 2011 at 8:28 Mark WaltersMark Walters 12.4k6 gold badges35 silver badges48 bronze badges 4
  • why does fnArray has only one big string in it ? – Adham Atta Commented Jun 7, 2011 at 8:36
  • I know i wrote at the top of the question that i understood. This isn't an issue for what this is being used for. Thank you – Mark Walters Commented Jun 7, 2011 at 8:36
  • No Adham, sorry my mistake, only just realised i've missed out a whole load of "" each side of the names. Thanks – Mark Walters Commented Jun 7, 2011 at 8:57
  • I've tried them all and they all work. so many thanks. I've only been using javascript for a couple of months so occassionaly i need a bit of a push to find a solution for something that might seem simple. Again thanks – Mark Walters Commented Jun 7, 2011 at 9:18
Add a ment  | 

4 Answers 4

Reset to default 2

You can get the index of the correct user

var unArray = ["markwalt", "jongossy", "lisacain", "jenndemp"];
var pwArray = ["mark1234", "flomaygo", "lisa1234", "jenny1234"];
var fnArray = ["Mark Walters, Jonathan Goss, Lisa Cain, Jenny Dempsey"];
var index = 0;
for (var i=0; i <unArray.length; i++) {
    if ((un == unArray[i]) && (pw == pwArray[i])) {
        valid = true;
        index = i;
        break;
    }
}

now you can access the correct data using

unArray[index]; 
// and so on for other arrays

Define a variable for full name, and set it if you have the valid user:

var fn = "";

/* ... */

valid = true;
fn = fnArray[i];

/* ... */

document.getElementById("mandatory1").value = fn;

Note: Actually you can check validity later on using fn. If it is empty string, then no user was logged in. This makes it have same purpose as valid, and more.

Try this.

function validate() {
    var un = document.getElementById("usern").value;
    var pw = document.getElementById("pword").value;
    var valid = -1;

    var unArray = ["markwalt", "jongossy", "lisacain", "jenndemp"];
    var pwArray = ["mark1234", "flomaygo", "lisa1234", "jenny1234"];
    var fnArray = ["Mark Walters","Jonathan Goss","Lisa Cain","Jenny Dempsey"];

    for (var i=0; i <unArray.length; i++) {
        if ((un == unArray[i]) && (pw == pwArray[i])) {
            valid = i;
            break;
        }
    }

    if (valid != -1) {
        alert ("Login was successful");
        document.getElementById("mandatory1").value = fnArray[valid];
    }
    else {
        alert("Invalid Username and/or Password! Please try again. You will not be able to submit this form without a successful login")
        document.getElementById("pword").value = "";
        document.getElementById("usern").value = "";
        document.getElementById("usern").focus();
    }
}

set mandatory1 when the login is successful based on i (in the for loop)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745664033a4639014.html

相关推荐

  • Creating a login in javascript - array comparison - Stack Overflow

    I have a login box using a simple javascript login paring usernames and passwords, before you all start

    4小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信