Jump to content

Having Trouble Preventing Form Submition


MrFish

Recommended Posts

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

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

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

  • 2 weeks later...

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...