MrFish Posted September 13, 2011 Share Posted September 13, 2011 I'm trying to notify the user submiting a form if they didn't enter the passwords correctly. The trouble is that the onsubmit function only gets half way through (tracked by alerts) before it just submits anyway. frm = document.getElementById("registerForm"); frm.onsubmit = function(e) { e = e || window.event; elements = frm.elements; password1 = elements["password1"]; password2 = elements["password2"]; if(password1.value != password2.value) { alert("The Passwords Entered Did Not Match"); e.preventDefault(); } else return true; } It get's just after this line password1 = elements["password1"]; and submits the form without completing. What can I do to make it run all the way through? Link to comment Share on other sites More sharing options...
Fmdpa Posted September 13, 2011 Share Posted September 13, 2011 Yeah, that is kinda confusing. I don't see any parse errors in your code. What I'd recommend is checking your error console if you haven't already because if there is an error, it will submit the form. Link to comment Share on other sites More sharing options...
justsomeguy Posted September 13, 2011 Share Posted September 13, 2011 You should be declaring all of your variables as local variables, not using global variables. When you set the elements variable you're overwriting window.elements because you're not declaring the variable as a local variable. The only reason you ever want to use a global variable is if you are explicitly trying to use the global variable. If you're not, then always use a local variable. I don't know if that's going to solve your problem, but it's something I notice whenever I see code like that. Link to comment Share on other sites More sharing options...
Synook Posted September 14, 2011 Share Posted September 14, 2011 You should probably return false if the validation fails, just in case. Link to comment Share on other sites More sharing options...
MrFish Posted September 22, 2011 Author Share Posted September 22, 2011 Thanks for the replies. I should have posted my solution sooner but here is what I found works- <form onsubmit="return validateForm(this);">...</form> It will run the function fully if you do something like this instead. Still not sure why the original didn't. Link to comment Share on other sites More sharing options...
thescientist Posted September 22, 2011 Share Posted September 22, 2011 Thanks for the replies. I should have posted my solution sooner but here is what I found works- <form onsubmit="return validateForm(this);">...</form> It will run the function fully if you do something like this instead. Still not sure why the original didn't. there were a number of reasons proposed. over writting the elements global variable, and not returning false, on top of returning the onsubmit handler itself. You never actually posted if you were returning within the content of the form tag itself. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.