3.2.0

var with multiple possible values? (javascript newbie here)

I have been trying to get a beginner's grasp of javascript, with no previous programming experience. Please excuse me if this question is just toooo dumb, but I have tried all day to find answers elsewhere.

PLEASE don't tell me about whole different ways of doing this (mySQL, php etc). For all kinds of reasons, this is the method I am stuck with.

I have a form in which people enter a user name and password.

The form is on a page which calls a .js file, and which contains valid username and password info.

IF the user types a username and password corresponding with the values in the .js file, then a cookie is written and they are able to view subsequent pages. If not, they are redirected to an error page.

It all works fine if there is just one option for username and password. However I need to allow for six possible usernames and passwords. Have tried all sorts of things, but I just don't have the chops to get this to work.

Can anyone help? I feel like this SHOULD be so easy! It's driving me nuts.

A sample of the code is here:
http://www.gosh.net.nz/workingwise/goshnetsample.js
Peter Hall
August 31,
You would need to use an array -

var valid =
[
    ["user1","password1"],
    ["user2", "password2"],
    ["user3", "password3"],
    ["user4", "password4"]
]


Then use JS functions to iterate through the array to find the matches. A simple guide to these can be found here -

http://www.devguru.com/Technologies/Ecmascript/Quickref/array.html

Note that this approach has very poor security. Your form should really call a CGI script.
Anthony
September 1,
Cheers Anthony, and thanks for the link. I'd managed to get that far, tried it in a number of ways, but couldn't get the actual processing to work after saying what the possible values might be. Could you possibly be a bit more specific about what's required? Is it something like creating a string then seeing if the value entered in the form is included in the string? ie what methods would I use? Sorry, I'm right at the beginning of the slope here.
Peter Hall
September 1,
You'd use the indexOf() method to find a matching string. If the two dimensional array is too complicated for you, try using two separate string arrays and use the index of the name match to find the matching password.

Here's some code that might make it a little easier for you -

http://stackoverflow.com/questions/143847/best-way-to-find-an-item-in-a-javascript-array

It doesn't quite do what you want since it searches for objects. You'd want to do a string comparison (by using indexOf()) and then return the index of the array position that matches so that you can know which corresponding element in the password array to check.

That code also creates prototypes, which you don't need. But the for() loop code is. So something as simple as -

var name = [...]
var password =  [..]
var passwordindex = -1

for (var i = 0; i < name.length; i++)
{
    if (name[i].indexOf(enteredname) == 0)
       passwordindex = i
 }

if (passwordindex > -1)
{
   if (password[passwordindex].indexOf(enteredpassword) == 0)
     /* You have a match here */
}
/* No match found */


where 'enteredname' and 'enteredpassword' are the ones the user enters.
Anthony
September 1,
Thanks a heap Anthony. And also for your earlier advice about cgi and security. I shall now try and bring it altogether.
Peter Hall
September 1,
Note that you'd probably want to break out of the for loop as soon as you find a match (or do the password check in situ). You may also need to use one of the case methods (enteredname.toLowerCase()) if mixed case is an issue. Set your name array to one case and convert to the same case before you test. More details here -

http://www.devguru.com/Technologies/Ecmascript/Quickref/string.html

The CGI script approach is the better way of handling it since it happens on the server and end users can't read the actual CGI script. Any user can view the source code of a page running on their client and find the names and passwords. I just googled for "html login" and found plenty of examples. I suggest you look at some of those.
Anthony
September 2,

This topic is archived.

See also:


Back to support forum