Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-11-2013, 07:18 PM   PM User | #1
diceman93
New to the CF scene

 
Join Date: Jan 2013
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
diceman93 is an unknown quantity at this point
Looping help.

Hey guys, i'm fairly new to programming in Javascript.
I've been trying to write a basic user name and password program.
I was wondering if I could get some help with how I would put it in a loop so if the information in incorrect, it will prompt up "x" amount of times until the information is correct. Thanks, here's what I have so far.

var user_name = prompt ("Please enter your username: ");
var password = prompt ("Please enter your password: ");

if( (user_name=="John123") && (password== "helloworld") ){
alert("Welcome, " + user_name + ".");
}
else{
alert (" Sorry, information invalid. " );
}

Last edited by diceman93; 01-11-2013 at 07:49 PM..
diceman93 is offline   Reply With Quote
Old 01-11-2013, 07:50 PM   PM User | #2
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 951
Thanks: 7
Thanked 98 Times in 98 Posts
WolfShade is an unknown quantity at this point
If you're doing this just to learn JavaScript, I'm all about it. HOPEFULLY you will not actually be trying to use this as a secure way to log in to something. JS is wonderful, but it is NOT secure.

Code:
var user_name, password;
for(var i=0; i<2; i++){
    user_name = prompt ("Please enter your username: ");
    password = prompt ("Please enter your password: ");

    if( (user_name=="John123") && (password= "helloworld") ){
              alert("Welcome, " + user_name + ".");
              break;
    }
    else{
         if(i<2){
          alert (" Sorry, information invalid. " );
           }
         else{
           alert("You've used up all your attempts.");
            break;
            }
    }
Untested, but I think it's correct.
__________________
^_^

If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
*
The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".
WolfShade is online now   Reply With Quote
Old 01-11-2013, 08:21 PM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Missing a final } at the very end of the code, to close the for loop.

And the message "You've used up all your attempts." will *NEVER* be seen.

That's because it is only seen (a) INSIDE the for loop and (b) when i is 2 or greater. But i can never be 2 or greater inside that loop, by the very terms of how the for is written.

Try again?
Code:
var user_name, password, passed = false;
for(var i=1; i<= 3 && passed == false; i++)
{
    user_name = prompt ("Please enter your username: ");
    password = prompt ("Please enter your password: ");

    passed = ( (user_name=="John123") && (password= "helloworld") );
} // end of loop after 3 tries *OR* if passed is true!

if ( passed )
{
    alert("Welcome, " + user_name + ".");
} else {
    location.href = "http://www.google.com";
}
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
diceman93 (01-11-2013)
Old 01-11-2013, 08:22 PM   PM User | #4
diceman93
New to the CF scene

 
Join Date: Jan 2013
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
diceman93 is an unknown quantity at this point
Perfect! Thanks alot Old Pedant. Makes sense now. Ya, that was it before too just missing the "}". Appreciate the help!

Last edited by diceman93; 01-11-2013 at 08:26 PM..
diceman93 is offline   Reply With Quote
Old 01-11-2013, 09:12 PM   PM User | #5
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,465
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Note that alert and prompt are for debugging use only and should never be used in a live web page.

Opera adds a "Disable JavaScript" option to all such dialogs (which if selected would stop the script running without returning from the first prompt) while Firefox, Safari and Chrome add an option to disable the display of subsequent dialogs when the second one is displayed (which if selected would stop the alert from displaying anything).
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 01-11-2013, 09:14 PM   PM User | #6
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Yep. alert() and prompt() and document.write() are just for "toy" scripts. [With a few exceptions in the case of document.write().]
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:11 PM.


Advertisement
Log in to turn off these ads.