Would `return false` block the client from sending requests to the client?

I wrote this in a few minutes so didn’t bother starting a whole Glitch project for it. Basically, it’s a simple validator that turns the dashes red if a bunch of spaces/nothing is sent, and green if text is entered. If I were to put this into a Node app and set the form action and everything, would return false; stop the form from sending text into the server?

Yes! In a <form>'s submit button onclick = return false; prevents the form from being submitted. Also, onsubmit = return false; in form tag does the same thing.

A better way to prevent the form submission would be like,

<form onsubmit="someFunc();" ...>
<script type="text/javascript">
function someFunc(event) {
  if(input == "red") event.preventDefault();
  // ... your code

Thanks! #10chars

  • there are other ways to submit a form, such as pressing enter on a text field. I think this doesn’t go through the click event on any submit button, but check me on that
  • you could work around this by setting disabled on the submit button, that’ll prevent the form from being submitted. but you’d have to know when to do this. maybe when input values change or something
  • you could alternatively do a similar return false in the form’s onsubmit, so that you can run some code in that same callback to determine whether to do so
  • a more declarative solution would be to use the form validation attributes available in modern browsers. search that up if you haven’t tried them before.
  • even if you code up the client to stop the submission, treat that as a convenience to the user, and imagine that they can turn off the validation if they feel like. an attacker can disable them or use something other than a browser to send submissions with invalid inputs.