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();" >
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.
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 .
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With