Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Don't submit form unless JavaScript is enabled

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

like image 664
proee Avatar asked Mar 09 '10 22:03

proee


People also ask

How can I submit a form without using JavaScript?

Use the <noscript></noscript> tags to define a "normal" submit-button when javascript is disabled. Show activity on this post. You could maybe use the <noscript> tag and encapsulate the above code with the button type as submit. If the client has js, the code inside the noscript will be ignored.

How do you restrict form submit on Enter?

Disallow enter key anywhere on("keydown", "form", function(event) { return event. key != "Enter"; }); This will cause that every key press inside the form will be checked on the key .

What causes JavaScript to prevent the form from submitting?

Vanilla JavaScript works purely on JavaScript without the use of any additional libraries. An event listener can be used to prevent form submission. It is added to the submit button, which will execute the function and prevent a form from submission when clicked.

Can you submit forms using JavaScript?

Generally, a form is submitted when the user presses a submit button. However, sometimes, you may need to submit the form programmatically using JavaScript. JavaScript provides the form object that contains the submit() method. Use the 'id' of the form to get the form object.


1 Answers

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.

like image 59
Peter Bailey Avatar answered Oct 19 '22 08:10

Peter Bailey