views:

197

answers:

6

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();" >

A: 

Why not just hide the submit button by default and show it with JavaScript? That won't stop a user from submitting the form by pressing the enter key, but it will trim the numbers. You can also show a message with a NOSCRIPT html element telling users to enable JavaScript.

Tom
A: 

One way to do it would be to have a hidden field defaulted to something like hunter2, and then when the form gets submitted, have javascript change it to something else. Then you can know if the user had javascript enabled.

FryGuy
A: 

Just get rid of any <input type="submit"> or <input type="image"> and replace them with buttons/images that trigger form submission using JavaScript. If there are no controls which trigger submission when JavaScript is disabled, you'll be reasonably sure that submission was from JavaScript. Keep in mind that clever users, broken browsers, and other weirdness will ensure that corrupt or invalid data gets submitted anyway.

Mr. Shiny and New
+2  A: 

Hiding the submit button won't work because the browser will try to submit the form if you press enter in an input type="text".

What you might want to try is adding the form element via JavaScript (i.e. don't have it in your HTML).

wrumsby
Doable but complicated: You would have to put the `form` around the existing form elements. For that, you would have to create the form element, attach it to the document, and move/copy the form fields into the element. I'm quite sure a file input field with a file selected won't survive that. If no file inputs are involved, it might be a feasible way.
Pekka
You could put the `input` fields in a `div`, then create the `form` and make the `div` the child of the `form`.
wrumsby
+3  A: 

The problem is that a form is submittable by default.

You can't take away the form's action attribute and add it later using JavaScript, because the browser will then use the current page as action, 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:

<form action="#">
  ....
</form>

this makes the form virtually unsubmittable at first, as the submission target is the current page.

You would then set the correct action attribute 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 #.

Pekka
If you use '#undefined', '#null' or '#n' as the action that will take away the jumpiness (as much as I hate contributing to evil ideas) :)
karim79
This is BAD for usability. The (non-js-enabled) user will being trying to submit the form and have no idea why it isn't working. I realize this solution works, but I personally wouldn't use it.
Peter Bailey
@Peter totally true. The submit button would have to be hidden too, and `<noscript>` tags containing a big fat "please activate JavaScript" used to avoid confusing the user.
Pekka
I don't see the usability being that BAD - don't 99% of users have javascript enabled?
proee
@proee: I really don't care about those numbers. 1% of 100,000 users is still 1,000 people getting into trouble *without* knowing what's wrong. That's bad, especially considering the little effort it will cost you to include something sensible in a `<noscript>` element.
Marcel Korpel
That's incorrect. `<form action="#">` **will** trigger a form submission back to the server for the same page. You can verify this using an HTTP inspection tool like Fiddler or Charles.
Simon Lieschke
+5  A: 

If they need javascript to submit the form, then there's no point of even showing them the form.

In light of that, I'd recommend something like this

<noscript>
  You cannot register without javascript enabled.
</noscript>

<form id="registration" style="display: none">
  <!-- form stuff here -->
</form>

<script type="text/javascript">
  document.getElementById( 'registration' ).style.display = 'block';
</script>

It's a bit cheesy and a hack, but then so is requiring javascript for a simple web form.

Peter Bailey