Hello, There is multidimensional array which has stored several information about each record. From those 1st is name and 2nd is pswd

I am trying to write a function which would check name and pswd entered by user and check whether name exists in array or not. If it does then check whether pswd matches or no. If it does then let user go to next page else give an error.

I have array and all set. My while loop even works for 1st record in table. If user enters 1st name and wrong pswd, it gives error. If user enters 1st name n pswd for it then it works which it should do. But for any other record it doesnt work even if i put correct information because my while loop dont increment for some reason and doesn't check 2nd record or after that.

Please help me with it.. here's my function code

function Test(formtest)
{
   
 initialize_array(); //this calls the array which is already populated by all data
 
 
 var errmsg = "Invalid username or password!";
 var x = 0;

 while (x < arrUser.length)
 {
    
    if ((formtest.usrrname.value == arrUser[x][0]) && (formtest.pswd.value == arrUser[x][1])) //[row][column] so it checks whether 1st column and 2nd column values match to what user entered in 1st row then 2nd then 3rd until length of array.. if they match say true  
        return true;
    else
        {
            alert(errmsg);
            return false; //if dont match give error n return false
        }
        
    x++; //somehow it dont increment and only checks first row n none after that.. Need Help here!!!
 }

oh wow i got it working with different loop

here it is

function MainVal1(objForm)
{
   
 initialize_array();
 
 
 var errmsg = "Invalid username or password!";
 var x = 0;
 var y = 0;

  for (x=0; x<arrUser.length; x++)
  {
      if (objForm.usrrname.value == arrUser[x][0])
      {
          if(objForm.pswd.value == arrUser[x][1])
            {
              return true;
     }
  alert(errmsg);
  return false;
 }
 }
 }

but am still curious to know about why while loop is not working.. if anyone can help in that.. i will appreciate it

thank you guys

in the loop you have made the function works once because one return always happens fopr the first time either the if retrurns true or the else retuns false
you can do something like:
while (x < arrUser.length)
{
if ((formtest.usrrname.value == arrUser[x][0]) && (formtest.pswd.value == arrUser[x][1]))
return true;

X++;
}
var errmsg = "Invalid username or password!";
alert(errmsg);
return false;
by the way the last code you claim to work (if the opening and closing blocks are like you wrote)
works only if the user name is correct but the pass is not
if there both are incorrect nothing will happen

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.