I have a basic form that I’m using for member registration. I’m using the form onsubmit event to populate a hidden form field based on some of the form’s fields. It’s working great, but I’d like to prevent the form from submitting if javascript is NOT enabled. This will ensure that the hidden field gets properly populated on the clients machine.
I realize it’s probably best practice to support registration without javascript enabled, but I’d like to run with the current solution I have in place
<form id='member_form' method="post" action="http://www.mysite.com/" onsubmit="build_username();" >
The problem is that a form is submittable by default.
You can’t take away the form’s
actionattribute and add it later using JavaScript, because the browser will then use the current page asaction, and the form can still be submitted.Also, removing the “submit” button doesn’t help, as the form still can be submitted using the enter key.
But, I submit to you (pun not intended!) this evil idea:
this makes the form virtually unsubmittable at first, as the submission target is the current page.
You would then set the correct
actionattribute using JavaScript.I can’t think of any reason why this wouldn’t work reliably across browsers, except that trying to submit it will make the browser jump to the top of the page because of the hash
#.