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.
  • 1
    Have you tried putting return Vslidation(); in the onclick? Iirc when false it will stop the action – Eric Nov 11 '16 at 12:17
  • @Eric If I put return false; in the onClick attribute it works as expected, but the above code in my OP doesn't work. Am I returning the false wrong somehow? – user24634 Nov 11 '16 at 12:30
  • 1
    See my answer. You are forgetting the return part in the onClick – Eric Nov 11 '16 at 12:43
  • Try removing the alert. – Eric Nov 11 '16 at 12:58

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

| improve this answer | |
  • While that does make sense, when I tried it doesn't seem to work. I have created a gist showing my current code gist.github.com/SamCousinsGB/81fba7c0ea5a0613703c928e2f77c9aa – user24634 Nov 11 '16 at 12:50
  • @SamC - Sorry I do not do/look at gists here. The whole point is to post your code here not make users goto an external link. – Eric Nov 11 '16 at 12:51
  • My mistake, I will edit my OP – user24634 Nov 11 '16 at 12:53

It's better to move the validation into the 'form' tag:

<apex:form id="form" onSubmit="return Validation();">
| improve this answer | |
  • It does prevent action from submitting. Validating the form assumes that every action requires the validation – Eric Nov 11 '16 at 12:40
  • @Eric, you right, I've tested it. The main point is that the result of Validation function must be returned. But I think validation on submit action is better because user can submit the form by pressing Enter button rather than clicking the button. – maovrn Nov 11 '16 at 12:44
  • Depending on the situation you are correct. But this would limit you to always performing the validation for every action. Sometimes a Form performs many actions and not every one of them could require validation (Like in actionfunction called from oncomplete...Good points nonetheless and a workable solution – Eric Nov 11 '16 at 12:46
  • Good point, I have no objections. – maovrn Nov 11 '16 at 12:50
  • Thank you for your input maovrn, this will be useful for another element of my project! – user24634 Nov 11 '16 at 13:03

You can make it work like this

<apex:commandLink value="Save" onClick="Validation();"/>

In your javascript

function Validation() 
    var isvalidForm = true;
    //Validation code goes here
    if(Form is not valid)
        isvalidForm = false;

In your visualforce page

<apex:actionFunction name="Actionfunc" action="ActionMethod"/>

Hope this will solve your problem, ActionMethod gets executed only when it returns true. When it returns false ActionMethod will not be callled.

| improve this answer | |
  • While this works it adds unnecessary elements/complexity to the page – Eric Nov 11 '16 at 13:16
  • Elements are added on the cost of functionality, that can't be achieved through the way you told. It provides the better client-side form validations. – Saran Dev Nov 11 '16 at 13:38
  • Um. The answer I provided works just fine. And no your answer does not provide any better validation it just modifies the flow – Eric Nov 11 '16 at 13:39
  • Yeah I know, but your answer is not providing any validation. – Saran Dev Nov 11 '16 at 13:42
  • The question was not about how to validate inputs and the op has a function for that. And for what it's worth, this answer does not provide any validation – Eric Nov 11 '16 at 13:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy