-1
<script>
function someFunc() {
    if (1==2) {
        return true;
    } else {
        alert("Not submitting");
        return false;
    }
}
</script>

This is from stackoverflow here

prevent form submission (javascript)

I want to know what the purpose of the obvious false statement 1==2 returns true is for?

Community
  • 1
  • 1
pearlescentcrescent
  • 143
  • 1
  • 3
  • 10
  • 2
    It's dummy, placeholder code. They wanted a function that would *always* return `false` (for testing); real validation code would replace `1 == 2` later. – Paul Roub Feb 28 '15 at 16:37

1 Answers1

0

In general, to stop a form from submitting you would need to do two things:

  • Place an "onsubmit" event on the form tag
  • define the event handler in javascript and return false in that function

So, basically, in HTML you would need:

<form ... onsubmit="return someFunc()">

And in the javascript code you would need:

function someFunc() {
    return false;
}

The part with 1 == 2 its for developing purposes only i would imagine, just a mechanism of making the forms to never submit.

When moving on from the developing environment this should be done in a more clever configurable way.

You might as well have a function like this:

function someFunc() {
    if (1 > 0) {
        return true;
    } else {
        alert("Not submitting");
        return false;
    }
}

That validation is only meant to always stop the from from submitting.

Or, a function like this:

function someFunc() {
    alert("Not submitting");
    return false;
}
Alexandru Godri
  • 528
  • 3
  • 7