I need to display a message on vf page based on onchange of data from picklist. In my controller the method is boolean type....based on data selected from picklist which call my method...if the method return true a alert message "true" will be displayed else if it return false message will display "false". Please help with a sample code.


3 Answers 3


To display error messages in the Visualforce page add below tag where you want to display the error message.

Visualforce page:

<apex:pageMessages />

Apex Controller:

ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error Message.');


ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Error Message.'));
  • 1
    When your answer does not provide more specific information than answers already present on the site it's preferable to refer to such answers and duplicate questions. This means we only have to maintain correct information in a single location. Feb 25, 2014 at 10:06

Another approach to show alert on visualforce page.

public class
 // value referred on visulforce to make decision
 public boolean returnValue {get; set;}

 // selectoption implementation
 public List<SelectOption> itemList {get; set;}
 public String selectedItem {get; set;}

 // Called from action function
 public void controllerMethod()
    // implement any logic using selectedItem 
    returnValue = !returnValue;
    return returnValue;

Alert message can be shown by using oncomplete attribute of actionfuntion by referencing boolean value directly in the javascript function ( showAtert(t)) called by this attribute.


         <apex:selectList value="{!selectedItem}" onchange="callMethod()">
           <apex:selectOption value="{!itemList}"
       <apex:actionfunction name="callMethod" action="{!controllerMethod}" oncomplete="showAlert({!returnValue })"/>
    function showAlert(t) {
      if(t) {
      } else {
        alert('No value entered !');
  • 2
    Adding an explanation rather than code dumping would improve the quality of your answer. Feb 25, 2014 at 10:14

Visualforce page

<!-- Using java script in visualforce page -->
<apex:page controller="DynamicVisualforceController">
  <apex:form >
        <apex:pageBlock >
           Input Here  : <apex:inputText id="inptID"/>
                       <apex:commandButton onclick="MyjavaFunction('{!$Component.inptID}')" value=" Submit "/>

  <!-- Java script starts Here -->
   function MyjavaFunction(ReceiveInputID){

    var inputValue = document.getElementById(ReceiveInputID).value;
         if(inputValue == ''){
            alert('You did not eneter any value in input box');
            alert(' You entered :: '+inputValue); 


Here is one code which may be useful for u


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .