Thursday 15 May 2014

Javascript Validation for SelectList in Salesforce by Accessing DOM ID



Problem:

If you choose SelectList Value as --none-- it needs to alert a message to the user.

Solution:
  1. <apex:page controller="SelectListController">
  2.     <script>
  3.     function Opportunity_Validation()
  4.     {
  5.         var eterm=document.getElementById('{!$Component.RenewalForm:configblock:configsecblock:configexpire:expterms}').value;
  6.        
  7.         if(eterm=='0')
  8.         {
  9.             alert('Select Expite Term');
  10.         }
  11.        
  12.     }
  13.     </script>
  14.    
  15.     <apex:form id="RenewalForm">
  16.         <apex:pageBlock id="configblock">
  17.             <apex:pageBlockSection title="Configuration Option" columns="2" id="configsecblock">
  18.                
  19.                 <apex:pageBlockSectionItem id="configexpire">
  20.                     <apex:outputLabel value="Expiry Term:" for="expireterm"/>
  21.                     <apex:selectList value="{!ExpireTerms}" multiselect="false" size="1" id="expterms" onchange="Opportunity_Validation();">
  22.                         <apex:selectOption itemValue="0" itemLabel="--none--"/>
  23.                         <apex:selectOption itemValue="1" itemLabel="1 Year"/>
  24.                         <apex:selectOption itemValue="3" itemLabel="3 Year"/>
  25.                         <apex:selectOption itemValue="5" itemLabel="5 Year"/>
  26.                     </apex:selectList>
  27.                 </apex:pageBlockSectionItem>
  28.             </apex:pageBlockSection>
  29.         </apex:pageBlock>
  30.     </apex:form>
  31. </apex:page>

Note:

Since JavaScript all identifiers are case sensitive. So make sure while initializing identifiers.

The variables lastName and lastname, are two different variables.

The functions myFunction and myfunction, are two different functions.

JavaScript does not interpret Var; as var.

No comments:

Post a Comment

Activities: Assign Tasks to a Queue Salesforce Lightning

Salesforce announced to assign Tasks to a Queue beginning from Spring'20 release. How does it work? In Setup, enter Queues in th...