+ Start a Discussion
ksmoksmo 

on the click of save button it should check condition and popup message and prevent save

I have a button update as below

<apex:commandButton id="UpdateButton" value="UPDATE" styleClass="UpdateButton" onclick="CheckAcceptOnUpdate()" action="{!doSave}"/>

on the apex class the do save does the update to the database perfectly.

But now I want that update to work only if accept checkbox is checked so I have written the onclick function

function CheckAcceptOnUpdate()
{


var accept = document.getElementById('{!$Component.formterm.terms}').checked;


  if(accept==false)
  {
  
  alert("please accept terms");
  
  
  }

}

The popup is working as expected and it is not preventing the update function.the action dosave is being called. If the accept checkbox is not checked, it should only show the pop up and do nothing else.

 
Best Answer chosen by ksmo
Prateek BhattPrateek Bhatt
Hi Ksmo,

Please use below code to prevent the calling of update function if checkbox is unchecked.
<apex:commandButton id="UpdateButton" value="UPDATE" styleClass="UpdateButton" onclick="return CheckAcceptOnUpdate();" action="{!doSave}"/>
				
function CheckAcceptOnUpdate()
{
	var accept = document.getElementById('{!$Component.formterm.terms}').checked;
	if(accept==false)
	{
 		alert("please accept terms"); 
        }
	return accept;
}


Thanks,

All Answers

Prateek BhattPrateek Bhatt
Hi Ksmo,

Please use below code to prevent the calling of update function if checkbox is unchecked.
<apex:commandButton id="UpdateButton" value="UPDATE" styleClass="UpdateButton" onclick="return CheckAcceptOnUpdate();" action="{!doSave}"/>
				
function CheckAcceptOnUpdate()
{
	var accept = document.getElementById('{!$Component.formterm.terms}').checked;
	if(accept==false)
	{
 		alert("please accept terms"); 
        }
	return accept;
}


Thanks,
This was selected as the best answer
ksmoksmo
Perfect..Thanks it worked!!!