Alert message on vf page

  • 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.

    possible duplicate of Difference between the multiple messaging options in Visualforce?. That question will show you what options exist for propagating messages from your controller to your VF page. How to validate picklist values in apex/validation rule appears to be a different question better asked separately.

  • 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.'));

    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.

  • 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


  • 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 !');

    Adding an explanation rather than code dumping would improve the quality of your answer.

License under CC-BY-SA with attribution

Content dated before 7/24/2021 11:53 AM