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!!!
 }

Dani AI

Generated

Short version: was right — a return inside the loop stops the function on the first iteration, so the loop never checks later records. 's switch to a loop that only returns after all records are checked fixes that. Below is a tidy, different pattern that does the same thing but is less error-prone and easier to read.

Use an array method that tests every record and returns a single boolean. This keeps the single “invalid” response outside the search and avoids accidental early exits.

// assume `users` is an array of [username, password] pairs
function validateForm(form) {
  var name = (form.usrrname.value || '').trim();
  var pass = form.pswd.value || '';

  var ok = users.some(function(pair) {
    return pair[0] === name && pair[1] === pass;
  });

  if (ok) return true;

  alert('Invalid username or password!');
  return false;
}

Quick checklist and tips:

  • Make sure initialize_array() (or whatever fills users) runs before validateForm is invoked.
  • Confirm form field names (usrrname, pswd) are spelled exactly as used in the code.
  • Use === for exact matches and trim() the input to avoid accidental whitespace mismatches.
  • If usernames should be case-insensitive, compare name.toLowerCase() against a lowercased stored value.
  • For debugging, log the array and its length (console.log(users, users.length)) and set a breakpoint in the loop to watch values.

Security note: Never rely on client-side credential checks for real authentication. Storing or checking plaintext passwords in browser code is insecure. Move authentication to the server, use hashed passwords, and always serve forms over HTTPS.

Recommended Answers

All 2 Replies

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.