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 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".
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.
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).