Welcome to Dream.In.Code
Getting Help is Easy!

Join 107,218 Programmers for FREE! Ask your question and get quick answers from experts. There are 1,843 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



User Name and password checking in arrays

 
Reply to this topicStart new topic

User Name and password checking in arrays

truetoon
post 11 Jun, 2008 - 03:37 AM
Post #1


New D.I.C Head

*
Joined: 22 May, 2008
Posts: 19

I am trying to write some code to check a users reader and password in an array and display the appropriate results based on their entries. I have made 2 attempts but cannot get either to function fully. The first attempt works but when i try to write in the responses for inavlid entries it goes wrong.
CODE

Attempt 1
var readerName
var readerPassword


readerName = window.prompt('Please enter your readerName','')  
  
for ( var count = 0; count < readerNameArray.length; count++)
{
    if(readerName == readerNameArray[count])
    {  
    readerPassword = window.prompt('please enter your password','')
        
        
        if(readerPassword == readerPasswordArray[count])
                {
                document.write('welcome')
                }
    }
    
}


the second attempt if more basic but it keeps writing out 'unknown Name' what ever the user enters.

CODE

Attempt 2
var readerName
var readerPassword


readerName = window.prompt('Please enter your reader name','')  
  
for ( var count = 0; count < readerNameArray.length; count++)
{
    if(readerName != readerNameArray[count])
    
    {  
    document.write('unknown name')
    }
        else
        {
        readerpassword = window.prompt('enter your password')
        }
        
    
    
}


this only being used as a project not for actual log on code for a website.

Any assistance would be greatly appreciated. smile.gif
User is offlineProfile CardPM

Go to the top of the page


atdrago
post 11 Jun, 2008 - 06:47 AM
Post #2


New D.I.C Head

*
Joined: 8 Jun, 2008
Posts: 27



Thanked 2 times
My Contributions


Did you declare your readerNameArray anywhere in there? I don't see it so I'm assuming it's declared outside of that code somewhere.

Also, it isn't needed, but it's just good practice to end each line with a semicolon (';').

And I'm not really sure why you have
CODE
readerName = window.prompt('Please enter your reader name','')
Do you need that second parameter?

If you fix all that and it still doesn't work then post the code where you declare the readerNameArray.

I hope that helps. smile.gif

Adam
User is offlineProfile CardPM

Go to the top of the page

truetoon
post 11 Jun, 2008 - 08:41 AM
Post #3


New D.I.C Head

*
Joined: 22 May, 2008
Posts: 19

QUOTE(atdrago @ 11 Jun, 2008 - 06:47 AM) *

Did you declare your readerNameArray anywhere in there? I don't see it so I'm assuming it's declared outside of that code somewhere.

Also, it isn't needed, but it's just good practice to end each line with a semicolon (';').

And I'm not really sure why you have
CODE
readerName = window.prompt('Please enter your reader name','')
Do you need that second parameter?

If you fix all that and it still doesn't work then post the code where you declare the readerNameArray.

I hope that helps. smile.gif

Adam


Here is the code with the arrays included, they are there just wasn't sure if they were required for any assistance.
CODE

var readerNameArray = ['ly-44', 'mr-7', 'rs-14', 'al-15', 'pn-44', 'wk-46'];

var readerPasswordArray = ['harding', 'grantly', 'proudie', 'slope', 'arabin',

                'bold'];

var readerName
var readerPassword


readerName = window.prompt('Please enter your readerName','')  
  
for ( var count = 0; count < readerNameArray.length; count++)
{
    if(readerName == readerNameArray[count])
    {  
    readerPassword = window.prompt('please enter your password','')
        
        
        if(readerPassword == readerPasswordArray[count])
                
                {
                document.write('welcome to the catalogue'+'<BR>'+'========================'+'<BR>')
                for(var countA = 0; countA < bookArray.length; countA ++)
                document.write(bookArray[countA] + '-------'+ authorArray[countA] + '<BR>')
                }
    }
    
}


i know about the semi colons i just always forget smile.gif.

i created the var readerName for the user entry. So the entered readerName is checked against the array for a valid entry, is this is not needed?

I probably should clarify that the code above works fine the probems arise when i try to include the code for invalid entries. So the readerName is not found in the array i want to write out 'invalid reader Name' and then the same for the password. I have tried to enter the else statement so it would somehting like this
CODE

if(readerName == readerNameArray[count])
    {  
    readerPassword = window.prompt('please enter your password','')
else
{
document.write('invalid readerName')


but it doesn't work!!! any ideas?

This post has been edited by truetoon: 11 Jun, 2008 - 11:18 AM
User is offlineProfile CardPM

Go to the top of the page

BetaWar
post 11 Jun, 2008 - 11:23 AM
Post #4


#include <soul.h>

Group Icon
Joined: 7 Sep, 2006
Posts: 1,093



Thanked 37 times

Dream Kudos: 675
My Contributions


Here ya go:

CODE
<script>
var readerNameArray = ['ly-44', 'mr-7', 'rs-14', 'al-15', 'pn-44', 'wk-46'];

var readerPasswordArray = ['harding', 'grantly', 'proudie', 'slope', 'arabin', 'bold'];

readerName = window.prompt('Please enter your readerName','');
  
for(count = 0; count < readerNameArray.length; count++){
  if(readerName == readerNameArray[count]){  
    readerPassword = window.prompt('please enter your password','')
    if(readerPassword == readerPasswordArray[count]){
      document.write('welcome to the catalogue<BR>========================<BR>')
      for(countA = 0; countA < bookArray.length; countA++){
        document.write(bookArray[countA] + '-------'+ authorArray[countA] + '<BR>')
      }
      break;
    }
  }
}
</script>


One of the problems you were having is you have a lot of extra whitespaces adn line breaks in your code. One bad thing about javascript is that it toally freeks out if there are linebreaks in function calls, outputs or arrays/variables.

The above code works for me in IE6.
User is offlineProfile CardPM

Go to the top of the page

phak588
post 12 Jun, 2008 - 12:37 AM
Post #5


New D.I.C Head

*
Joined: 11 Jun, 2008
Posts: 2


My Contributions


Hi,

Thanks for your reposnse however I am not performing a password check.

My problem
I have an array of books
I have an array of Due Dates as integers from 1st Jan
I have an array of Status which shows which book in on loan to which reader
I have an array of the book cost from which I work out a fine.

What I am tryin to do is sum up all the fines for borrowed books against a reader so I can produce something like
Reader rs-14 is fined £134.00
Reader ly-14 is fined £36.24

etc
My logic I am sure is wrong. I am strugling to figure out the loops I need to perform to work this out.
User is offlineProfile CardPM

Go to the top of the page

truetoon
post 12 Jun, 2008 - 01:01 AM
Post #6


New D.I.C Head

*
Joined: 22 May, 2008
Posts: 19

QUOTE(phak588 @ 12 Jun, 2008 - 12:37 AM) *

Hi,

Thanks for your reposnse however I am not performing a password check.

My problem
I have an array of books
I have an array of Due Dates as integers from 1st Jan
I have an array of Status which shows which book in on loan to which reader
I have an array of the book cost from which I work out a fine.

What I am tryin to do is sum up all the fines for borrowed books against a reader so I can produce something like
Reader rs-14 is fined £134.00
Reader ly-14 is fined £36.24

etc
My logic I am sure is wrong. I am strugling to figure out the loops I need to perform to work this out.


Phak88 this is not your post yours is futher down the forum, looks like we are doing the same course though????
User is offlineProfile CardPM

Go to the top of the page

truetoon
post 12 Jun, 2008 - 02:29 AM
Post #7


New D.I.C Head

*
Joined: 22 May, 2008
Posts: 19

This is where i am up to so far, the code works apart from the code to respond to a invalid reader name.

It works through the code asking for the readername, if an invalid reade name is entered it writes out 'unknown name'. If a valid reader name is entered it asks for the password. If a valid password is entered it writes out the array but with 'Unknown name' on the last line. I have tried making the end statement an else statment but the results are even worse.

CODE

var readerNameArray = ['ly-44', 'mr-7', 'rs-14', 'al-15', 'pn-44', 'wk-46'];
var readerPasswordArray = ['harding', 'grantly', 'proudie', 'slope', 'arabin',
'bold'];

var readerName
var readerPassword



readerName = window.prompt('Please enter your readerName','')
{
      for ( var count = 0; count < readerNameArray.length; count++)
    {
    if(readerName == readerNameArray[count])
    {  
    readerPassword = window.prompt('please enter your password','')
    {    
        
        if(readerPassword == readerPasswordArray[count])
                
                {
                document.write('welcome to the catalogue'+'<BR>'+'========================'+'<BR>')
                for(var countA = 0; countA < bookArray.length; countA ++)
                document.write(bookArray[countA] + '-------'+ authorArray[countA] + '<BR>')
                }
        else
        {document.write('invalid password')}
    }
    
}
}
};
if(readerName != readerNameArray[count])
{
document.write('unknown Name')
}


This is probably really basic stuff but i am really new to this and cannot seem to get my head around the problem

User is offlineProfile CardPM

Go to the top of the page

atdrago
post 12 Jun, 2008 - 07:26 AM
Post #8


New D.I.C Head

*
Joined: 8 Jun, 2008
Posts: 27



Thanked 2 times
My Contributions


Ah. I see the problem now. You're iterating through that array of user names, checking the name in the current spot of the array against what the user has input. Then you continue iterating to the end of array, and check it again against what the user has input. It's not working because the spot where the name is in the array won't be the same when you're at the end of the loop.

Instead, you could check the username every time you iterate one space through the loop, but if I understand what you're trying to do you won't want to do that because you'll get an error each time until you find the correct username.

What you could do is create a boolean, and once you find the correct user name set it to true. Then at the end check if that boolean is true, if it is do nothing, if it isn't output "unknown username."

Here's the code:
javascript
readerName = window.prompt('Please enter your readerName','') 
{
var isValid = false;
for ( var count = 0; count < readerNameArray.length; count++)
{
if(readerName == readerNameArray[count])
{
isValid = true;
readerPassword = window.prompt('please enter your password','')
{
if(readerPassword == readerPasswordArray[count])
{
document.write('welcome to the catalogue'+'<BR>'+'========================'+'<BR>')
for(var countA = 0; countA < bookArray.length; countA ++)
document.write(bookArray[countA] + '-------'+ authorArray[countA] + '<BR>')
}
else
{document.write('invalid password')}
}

}
}
if(!isValid)
{
document.write('unknown Name')
}
};


First you set the variable 'isValid' to false. When you know it is a valid username, set it to true. It will only be true if the username is valid. Then at the end check to see is 'isValid' is false. If it is, the username is invalid.

Hope that helps. smile.gif
User is offlineProfile CardPM

Go to the top of the page

truetoon
post 12 Jun, 2008 - 08:24 AM
Post #9


New D.I.C Head

*
Joined: 22 May, 2008
Posts: 19

thanks Atdrago i also sorted the problem the following way

CODE

var readerPasswordArray = ['harding', 'grantly', 'proudie', 'slope', 'arabin',

'bold'];

var readerName
var readerPassword

document.write('BARSETSHIRE COUNTY LIBRARIES' + '<BR>'+ '================================'+'<BR>')

readerName = window.prompt('Please enter your readerName','')
{
      for ( var count = 0; count < readerNameArray.length; count++)
    {
    if(readerName == readerNameArray[count])
    {  
    readerPassword = window.prompt('Please enter your password','')
    {    
        
        if(readerPassword == readerPasswordArray[count])
                
                {
                document.write('welcome to the catalogue'+'<BR>'+'========================'+'<BR>')
                for(var countA = 0; countA < indexArray.length; countA ++)
                document.write(countA +'------'+ bookArray[countA] + '-------'+ authorArray[countA] + '--------' + '<BR>')
                }
        else
        {document.write('Invalid password')}
    }
    break;
}
}
}

if(readerName != readerNameArray[count])//statement for invalid reader name
{
document.write('Unknown name')
}


</SCRIPT>

</HEAD>

<BODY>

</BODY>

</HTML>


I simply added in the break.
i will get there in the end, i hope!!

This post has been edited by truetoon: 12 Jun, 2008 - 08:25 AM
User is offlineProfile CardPM

Go to the top of the page

atdrago
post 12 Jun, 2008 - 09:11 AM
Post #10


New D.I.C Head

*
Joined: 8 Jun, 2008
Posts: 27



Thanked 2 times
My Contributions


Yup, that'd work too.
User is offlineProfile CardPM

Go to the top of the page

cavvysri
post 12 Jun, 2008 - 10:28 AM
Post #11


New D.I.C Head

*
Joined: 12 Jun, 2008
Posts: 29


My Contributions


QUOTE(truetoon @ 12 Jun, 2008 - 08:24 AM) *

thanks Atdrago i also sorted the problem the following way

CODE

var readerPasswordArray = ['harding', 'grantly', 'proudie', 'slope', 'arabin',

'bold'];

var readerName
var readerPassword

document.write('BARSETSHIRE COUNTY LIBRARIES' + '<BR>'+ '================================'+'<BR>')

readerName = window.prompt('Please enter your readerName','')
{
      for ( var count = 0; count < readerNameArray.length; count++)
    {
    if(readerName == readerNameArray[count])
    {  
    readerPassword = window.prompt('Please enter your password','')
    {    
        
        if(readerPassword == readerPasswordArray[count])
                
                {
                document.write('welcome to the catalogue'+'<BR>'+'========================'+'<BR>')
                for(var countA = 0; countA < indexArray.length; countA ++)
                document.write(countA +'------'+ bookArray[countA] + '-------'+ authorArray[countA] + '--------' + '<BR>')
                }
        else
        {document.write('Invalid password')}
    }
    break;
}
}
}

if(readerName != readerNameArray[count])//statement for invalid reader name
{
document.write('Unknown name')
}


</SCRIPT>

</HEAD>

<BODY>

</BODY>

</HTML>


I simply added in the break.
i will get there in the end, i hope!!



This post has been edited by cavvysri: 12 Jun, 2008 - 10:34 AM
User is offlineProfile CardPM

Go to the top of the page

baggins
post 14 Jun, 2008 - 10:00 AM
Post #12


New D.I.C Head

*
Joined: 14 Jun, 2008
Posts: 14


My Contributions



Hi everyone,

I'm new on the website smile.gif

It seems that I have the same project, and I'm stuck too, I'm a little farther on the question. My problem is that I have my first 'if' statement working fine but the second is not responding properly. Instead of writing: no such book, it wrote book reserved+no such book cool.gif

can someone please help me blink.gif rolleyes.gif

Here is my coding:

var bookArray = ['Framley Parsonage 1st Ed', 'Lady, Don\'t Fall Backwards', 'How to Win Friends 2nd Ed ', 'The Death of Harry Potter', 'The Kama Sutra (unexpurgated)', 'Little Noddy Goes to the Moon'];
var IndexBookArray = [ 0, 1, 2, 3, 4, 5 ]

var IndexBookArray;
IndexBookArray = window.prompt('What would you like to do?' + '1. Reserve a book' + '2. Borrow a book' + ' .Enter 1 or 2 to select','')

if (IndexBookArray == 1)

IndexBookArray = window.prompt('Enter the index number of the book to be reserved','')

if((IndexBookArray == 0) || (IndexBookArray == 2) ||(IndexBookArray > 3) || (IndexBookArray < 6))
{
document.write('Book reserved')
}

if (IndexBookArray > 5)
{
document.write('--->' + 'No such book!')
}
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 8/28/08 01:41AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month