davej Posted December 9, 2012 Share Posted December 9, 2012 In my code I have not had any use for the onsubmit event. When my javascript initializes I just change the submit button into an ordinary button and then have the onclick event call the validation function which may or may not submit the form. But I would like to understand this example... http://www.w3schools.com/js/js_form_validation.asp Apparently onsubmit="return function1()" gives function1 the ability to cancel the submittal by returning a false value. Is this approach in any way better than what I am doing? Are there yet other ways to control form submittal on the client side? Thanks. Link to comment Share on other sites More sharing options...
satishpoul Posted December 10, 2012 Share Posted December 10, 2012 <input type="button" name="some_name" onclick="your_function();" />or post your code to understand your problem briefly Link to comment Share on other sites More sharing options...
Ingolme Posted December 10, 2012 Share Posted December 10, 2012 Onsubmit is the only way to ensure that the form sending is cancelled. Usually it's not an attribute, but set on the form using Javascript. Link to comment Share on other sites More sharing options...
thescientist Posted December 14, 2012 Share Posted December 14, 2012 yeah, just using an event handler for the form's onsubmit event would be the cleanest way. Link to comment Share on other sites More sharing options...
ala888 Posted July 1, 2014 Share Posted July 1, 2014 why does it need to be onsubmit="return function()" and not just simply onsumbit="function()" ?if the function() already returns false, why do we need to add another return there ? Link to comment Share on other sites More sharing options...
davej Posted July 2, 2014 Author Share Posted July 2, 2014 I have not tested this but it would be the same as... <form name="myForm" action="demo_form.asp" onsubmit="validateForm();return false;" method="post"> ...which of course has to do with the cancellation of the default action. Link to comment Share on other sites More sharing options...
justsomeguy Posted July 2, 2014 Share Posted July 2, 2014 if the function() already returns false, why do we need to add another return there ?Because the code inside the attribute is basically its own function, which runs in the scope of the element with the attribute on it. When that event handler actually runs it's something more like this:function() { validateForm();}If there's no return statement, then the event handler doesn't return anything to cancel the default action. Adding the return statement fixes that:function() { return validateForm();}Now the event handler returns a value. Link to comment Share on other sites More sharing options...
dsonesuk Posted July 2, 2014 Share Posted July 2, 2014 You have not mentioned that you are relying on someone clicking the button that you are using for submit, they could easily bypass this by hitting the return/enter key while in a input field. Where as if you use onsubmit you would catch this. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now