Tuesday 27 May 2014

How to validate selectlist using javascript in visualforce page


Problem
When user select   --none-- from the select list, it must alert a message to the user using javascript on a button click.

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



Note:

1.    Since JavaScript all identifiers are case sensitive. So make sure while initializing identifiers.
2.    The variables lastName and lastname, are two different variables.
3.    The functions myFunction and myfunction, are two different functions.
4.  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...