开发者

Need help with authenticating a usrname & pswd through an array in javascript

开发者 https://www.devze.com 2023-02-24 00:30 出处:网络
Hi Everyone The question I\'m asking is mainly on Javascript. First, I\'m not very good with technical terms, so bear with me if I\'m not that clear.

Hi Everyone

The question I'm asking is mainly on Javascript. First, I'm not very good with technical terms, so bear with me if I'm not that clear.

Anyway, my goal is to authenticate a login process, the username and password, where both argument exist in an array, which is populated by a XML file. Basically the array contains information from an XML file and the username & password is also within that array. Inorder for a user to login, the username and password entered must be within the array or it returns an error message. My problem is that I'm not sure which syntax I am suppose to use when representing an index in the array and I'm not sure if the syntax I'm using is even correct. All I know is that it authenticated the first username and password in the array & XML file, but everything else is treated as incorrect even if it is correct.

Here's my Javascript file:

var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");

//DEFINE LOAD METHOD
function LoadXML(xmlFile)
{
 xmlDoc.load(xmlFile);
 xmlObj = xmlDoc.documentElement;
}

//declare & initialize array
var arrPerson = new Array()

//initialize array w/ xml
function initialize_array()
{
LoadXML("PersonXML.xml");
var x = 0;
while (x < xmlObj.childNodes.length)
{
    var tmpArr = new Array(xmlObj.childNodes(x).getAttribute("Usrname"), 
                           xmlObj.childNodes(x).getAttribute("Pswd"), 
                           xmlObj.childNodes(x).getAttribute("FirstName"), 
                           xmlObj.childNodes(x).getAttribute("LastName"), 
                           xmlObj.childNodes(x).getAttribute("DOB"),
                           xmlObj.childNodes(x).getAttribute("Gender"),  
                           xmlObj.childNodes(x).getAttribute("Title"));
    arrPerson.push(tmpArr);
    x++;   
}
}

//Validation
function LogInVal(objtxt)
{
    if(objtxt.value.length == 0)
    {
        objtxt.style.background = "red";
        return 1;
    }

    else
    {
        objtxt.style.background = "white";
        return 0;
    }
}

//main validation
function MainVal(objForm)
{
    var errmsg = "empty field";
    var errmsg2 = "Incorrect Username and Password";
    var msg = "You have logged in successfully";
    var errCount = 0;

    var usrname1 = document.getElementById("usrname1").value;
    var pswd1 = document.getElementById("pswd1").value;

    errCount += LogInVal(objForm.usrname);
    errCount += LogInVal(objForm.pswd);

    if (errCount != 0)
    {
        alert(errmsg);
        return false;
    }
    else
    {
        initialize_array();
        for (x = 0; x < arrPerson.length; x++) 
        {
            if (arrPerson[x][0] == usrname1 && pswd1 == arrPerson[x][1])  //I think my problem is within here
            {
                alert(msg);
                return true;
            }

            else
            {
                alert(errmsg2);
                return false;
            }
        }
    }

}

And here's my XML file:

<?xml version ="1.0" encoding="utf-8" ?>
<!--GGFGFGFVBFVVVHVBV-->
<PersonInfo>
  <Person Usrname="Bob111" Pswd="Smith111" personid="111" FirstName="Bob" LastName="Smith" DOB="01/01/1960" Gender="M" Title="Hello1"> 
  </Person>
  <Person Usrname="Joe222" Pswd="Johnson222" personid="222" FirstName="Joe" LastName="Johnson" DOB="12/01/1980" Gender="M" Title="Hello2">
  </Person>
  <Person Usrname="Tracey333" Pswd="Wilson333" personid="333" FirstName="Tracey" LastName="Wils开发者_如何学运维on" DOB="12/01/1985" Gender="F" Title="Hello3"> 
  </Person>
  <Person Usrname="Connie444" Pswd="Yuiy444" personid="444" FirstName="Connie" LastName="Yuiy" DOB="12/01/1985" Gender="F" Title="Hello4">
  </Person>
  <Person Usrname="Brian555" Pswd="Dame555" personid="555" FirstName="Brian" LastName="Dame" DOB="12/01/1985" Gender="M" Title="Hello5"> 
  </Person>
  <Person Usrname="Scott666" Pswd="Bikes666" personid="666" FirstName="Scott" LastName="Bikes" DOB="12/01/1985" Gender="MF" Title="Hello6">  
  </Person>
</PersonInfo>


Ok, Firstly. You should have mentioned this is an assignment and not a real world application. hehe.

Ok your problems is simple, in both branches of your if statement you RETURN, which effectively leaves the function on the first iteration. Only return when either the name is validated or you have run out of people to validate.

PS: It is a GOOD idea to learn this pattern well. It is used a LOT in programming in general

else
    {
        initialize_array();
        for (x = 0; x < arrPerson.length; x++) 
        {
            if (arrPerson[x][0] == usrname1 && pswd1 == arrPerson[x][1])  // This is fine
            {
                alert(msg);
                return true;
            }

            // We must not do this if we still have work to do. 
            //else
            //{
            //    alert(errmsg2);
            //    return false;
            //}
        }

        // Moved to outside the loop. IE: We have no one else to validate
        // therefore the username and password combo is invalid.
        alert(errmsg2);
        return false;

    }


Are you doing the login validation on the browser? If so that's not a good idea as a savy user could use something like Firebug or Fiddler to just see the usernames and passwords as you transmit them to the browser.

You need to do login validation on the server.

0

精彩评论

暂无评论...
验证码 换一张
取 消