[SalesForce] Block an action from executing via Javascript Function

I have the following commandLink

<apex:commandLink value="Save" onClick="Validation();" action="{!ActionMethod}" />

When it's clicked, I would like to stop the action from executing by returning false.

function Validation() {
        return false;
}

The above method does not work, but if I put return false directly in the onClick attribute the behaviour works as expected:

<apex:commandLink value="Save" onClick="return false;" action="{!ActionMethod}" />

Is there a way I can pass this return value up to the onClick attr?

Gist Edit

<apex:commandLink value="Save" onclick="return Validation();" action="{!saveSettings}"/>                        


function Validation() {              
  alert('returning false');   
  return false;                    
}


// Alert shows 'returning false', action still executes.

Best Answer

It should be like this:

<apex:commandLink value="Save" onClick="return Validation();" action="{!ActionMethod}" />

Add the return to the onClick. When Validation return false the onClick will then return false thus preventing the action from occurring