So I've spent at least 4 hours trying to get this script to work, and I'm at a loss. My Google-fu has failed me. With the following script (in spoiler) I have to " use a loop so that if the user enters incorrect data three times a message box comes up indicating that three is the limit and that they can no longer attempt log ons." I just need an alert box to pop up, I don't need to actually lock the user out. I know this is ridiculous and would never be used in real life, but this is what the professor wants. I'm sorry if this is an easy question, I've honestly learned nothing this entire semester because this guy doesn't seem to like to teach. Any help would really be appreciated.
Anybody want to help with a JavaScript assignment?
Hint: use the object that you actually sent instead of the word frmRegister, which you haven't defined before using. Also use Firebug on Firefox to check for yourself that the error starts on the first line of your code. in your validate(form) function. If not, just check where the error happens using Firebug. It will tell you which line in your page causes the error.
Edit: Sorry I mean second line in your function. First line is fine as it is just a variable declaration that looks fine.
It also looks like your while may be backwards. If I'm reading the definition of while correctly, it only creates the loop if the statement evaluates to true, and since attempts is set to 0 to begin with, there will be no iteration, since the statement that's supposed to loop with while is false at the beginning.
It would appear that Mozilla really likes JavaScript, because they have a full reference available fo' free, online. This doesn't look all that different from the VBA and Cocoa that I've dabbled in before. Definitely look at using the reference as a source besides just the book, because the online documentation is almost always ridiculously helpful, since it contains full definitions of every aspect of the code.
This is actually correct. This is a sample code for something else, but it demonstrates what you need to do with attempts:" @proggykins: I didn't look at it too closely, but when you do "attempts = attempts++" that's not doing what you want it to do. To increment a variable, you just do "attempts++;" depending on how JavaScript handles types and stuff, you're probably doing something weird to the attempts variable. Basically you're saying "hey, Attempts, your new value is the fact that you've incremented yourself." Attempts is then like "lolwut?" "
test1 : for (var i = 0; i < 3; i++) { //The first for statement is labeled "test1" test2 : for (var j = 0; j < 3; j++) { //The second for statement is labeled "test2" if (i == 1 && j == 1) { continue test1; } else { alert("i = " + i + ", j = " + j); } } } /*Output is: "i = 0, j = 0" "i = 0, j = 1" "i = 0, j = 2" "i = 1, j = 0" "i = 2, j = 0" "i = 2, j = 1" "i = 2, j = 2" Notice how it skips both "i = 1, j = 1" and "i = 1, j = 2"*/
</script>
I think I might have gotten it. It's pretty hobbled together, but it works...kind of.
</script>
Okay, okay, I see the problem with the while expression. A while statement is supposed to be used to create a continuous loop, starting with a true base case, and continuing until the statement is false.
You're actually trying to do the opposite. The while isn't supposed to execute until it's greater than three.
I think what you actually want is to use a for expression. Something like this:
It's a bit too late for me to debug this but from looking at it there's a few things that should be changed. First, attempts should be attempts = 0. It's automatically declared when the page is loaded, so you're really limiting the user to two errors rather than three. Second, replace the while loops with if statements like this:
I can't get the spoiler warning to work so here's the code:
function validate(){
var error = 0;
if (attempts => 3){
alert('Error Too many submissions');
}
else if (attempts < 3){
if (username.length < 6){
alert('error');
error++;
}
if (password1 != password2){
alert('error')
error++;
}
if (error){
attempts++;
}
}
}
Hopefully you get the gist of what I'm saying. I'm a bit constrained on time right now so I can't write the code out completely.
This is what you want.
</script> </head> <body> <form name="frmRegister" method="post" action="register.aspx" onsubmit="return validate(this);"> <div class="label"><label for="txtUsername">Username: </label></div> <div class="formElement"> <input type="text" name="txtUserName" id="txtUserName" size="12" /> </div> <div class="clear"></div>
<div class="label"><label for="txtPassword">Password: </td></label></div> <div class="formElement"> <input type="password" name="txtPassword" id="txtPassword" size="12" /> </div> <div class="clear"></div>
<div class="label"><label for="txtPassword2">Confirm your password: </label></div> <div class="formElement"> <input type="password" name="txtPassword2" id="txtPassword2" size="12" /> </div> <div class="clear"></div> <div class="label"> </label></div> <div class="formElement"><input type="submit" value="Log in" /></div> </form> </body> </html>
- Put the attempts outside of the function to make it a global variable.
- Put an if statement at the end so it will only increment once if at any point in the code returnValue was set to False. The way it was originally they could potentially get stuck in the loop after 1 attempt to log in.
- Send an alert to let the user know how many attempts have been made.
</script> </head> <body> <form name="frmRegister" method="post" action="register.aspx" onsubmit="return validate(this);"> <div class="label"><label for="txtUsername">Username: </label></div> <div class="formElement"> <input type="text" name="txtUserName" id="txtUserName" size="12" /> </div> <div class="clear"></div>
<div class="label"><label for="txtPassword">Password: </td></label></div> <div class="formElement"> <input type="password" name="txtPassword" id="txtPassword" size="12" /> </div> <div class="clear"></div>
<div class="label"><label for="txtPassword2">Confirm your password: </label></div> <div class="formElement"> <input type="password" name="txtPassword2" id="txtPassword2" size="12" /> </div> <div class="clear"></div> <div class="label"> </label></div> <div class="formElement"><input type="submit" value="Log in" /></div> </form> </body> </html>
Please Log In to post.
Log in to comment