+ Start a Discussion
sudhirn@merunetworks.comsudhirn@merunetworks.com 

Javascript validation is not firing

Hi, 

 I created a javascript validation on page it is firiring only for the first field and it is not firing for second field please suggest me what is the mistake in the code written
 
<apex:page controller="DRNewControllers" showHeader="false" sidebar="false" action="{!save}">


    <script>
        function setRequired() {
            if (document.getElementById('{!$Component.theForm.thePageBlock.CustPageBlockSection.CustPageBlockSectiontitle.customername}').value == '')
                alert("Enter Customer Name");
            else if (document.getElementById('{!$Component.theForm.thePageBlock.CustPageBlockSection.projectname}').value == '')
                alert("Enter Project Name"); 
            else if (document.getElementById('{!$Component.theForm.thePageBlock.CustPageBlockSection.address1}').value == '')
                alert("Enter Address 1");               
            else if (document.getElementById('{!$Component.theForm.thePageBlock.CustPageBlockSection.city}').value == '')
                alert("Enter City");
            else if (document.getElementById('{!$Component.theForm.thePageBlock.CustPageBlockSection.state}').value == '')
                alert("Enter State");
            else if (document.getElementById('{!$Component.theForm.thePageBlock.CustPageBlockSection.zip}').value == '')
                alert("Enter Zip");
            else if (document.getElementById('{!$Component.theForm.thePageBlock.CustPageBlockSection.country}').value == '')
                alert("Enter Country");
            else if (document.getElementById('{!$Component.theForm.thePageBlock.CustPageBlockSection.company}').value == '')
                alert("Enter Company");
            else if (document.getElementById('{!$Component.theForm.thePageBlock.CustPageBlockSection.industry}').value == '')
                alert("Enter Industry");         
                             
            else if (document.getElementById('{!$Component.theForm.thePageBlock.EndCustPageBlockSection.FirstName}').value == '')
                alert("Enter First Name");     
            else if (document.getElementById('{!$Component.theForm.thePageBlock.EndCustPageBlockSection.LastName}').value == '')
                alert("Enter Last Name"); 
            else if (document.getElementById('{!$Component.theForm.thePageBlock.EndCustPageBlockSection.Email}').value == '')
                alert("Enter Email ID"); 
            else if (document.getElementById('{!$Component.theForm.thePageBlock.EndCustPageBlockSection.Phone}').value == '')
                alert("Enter Phone");   

            else if (document.getElementById('{!$Component.theForm.thePageBlock.DealPageBlockSection.EstimatedClosedate}').value == '')
                alert("Enter Estimated Close Date");
            else if (document.getElementById('{!$Component.theForm.thePageBlock.DealPageBlockSection.ProjectOverview}').value == '')
                alert("Enter Project Overview");
            else if (document.getElementById('{!$Component.theForm.thePageBlock.DealPageBlockSection.Competitor}').value == '')
                alert("Enter Competitor");
            else if (document.getElementById('{!$Component.theForm.thePageBlock.DealPageBlockSection.ManageAPNET}').value == '')
                alert("Select Manage AP Networks");             

        }
    </script>  

         <apex:form id="theForm">
         <apex:pageBlock title="New Cloud Deal Registration Form" id="thePageBlock">
         
<apex:pageBlockButtons location="top">
      <apex:commandButton value="Submit Deal" action="{!Submit}" onclick="setRequired();"/>
      <apex:commandButton value="Cancel" immediate="false" action="{!Cancel}"/>
  </apex:pageBlockButtons>
  <apex:pageBlockButtons location="bottom">
      <apex:commandButton value="Submit Deal" action="{!Submit}"/>
      <apex:commandButton value="Cancel" immediate="false" action="{!Cancel}"/>
  </apex:pageBlockButtons>
  
   <apex:pageBlockSection title="End Customer Information" id="CustPageBlockSection">
    
   <apex:pageBlockSectionItem id="customernametitle">   
   <apex:outputLabel value="Customer Name" />
   <apex:outputPanel layout="block" styleClass="requiredInput">
   <apex:outputPanel layout="block" styleClass="requiredBlock" />
                    <apex:inputField value="{!newmember.Customer_Name__c}" required="false" id="customername"/>    </apex:outputPanel>
   </apex:pageBlockSectionItem>
       
   <apex:pageBlockSectionItem id="projectnametitle">    
   <apex:outputLabel value="Project Name" />
   <apex:outputPanel layout="block" styleClass="requiredInput">
   <apex:outputPanel layout="block" styleClass="requiredBlock" />  
         <apex:inputField value="{!newmember.Project_Name__c}" required="false" id="projectname"/>    </apex:outputPanel>       
   </apex:pageBlockSectionItem>    
       
   <apex:pageBlockSectionItem id="address1title">    
   <apex:outputLabel value="Address Line 1" />
   <apex:outputPanel layout="block" styleClass="requiredInput">
   <apex:outputPanel layout="block" styleClass="requiredBlock" /> 
        <apex:inputField value="{!newmember.Address_Line1__c}" required="false" id="address1"/>   </apex:outputPanel>
   </apex:pageBlockSectionItem>      
       
   <apex:inputField value="{!newmember.Address_Line2__c}" required="false" id="address2"/> 
   
   <apex:pageBlockSectionItem id="citytitle">    
   <apex:outputLabel value="City" />
   <apex:outputPanel layout="block" styleClass="requiredInput">
   <apex:outputPanel layout="block" styleClass="requiredBlock" />    
        <apex:inputField value="{!newmember.City__c}" required="false" id="city"/> </apex:outputPanel>
    </apex:pageBlockSectionItem>       
       
    <apex:pageBlockSectionItem id="statetitle">    
   <apex:outputLabel value="State" />
   <apex:outputPanel layout="block" styleClass="requiredInput">
   <apex:outputPanel layout="block" styleClass="requiredBlock" />
        <apex:inputField value="{!newmember.State__c}" required="false" id="state"/> </apex:outputPanel>
    </apex:pageBlockSectionItem>     
       
   <apex:pageBlockSectionItem id="ziptitle">    
   <apex:outputLabel value="Zip" />
   <apex:outputPanel layout="block" styleClass="requiredInput">
   <apex:outputPanel layout="block" styleClass="requiredBlock" /> 
        <apex:inputField value="{!newmember.Zip_Postal_Code__c}" required="false" id="zip"/> </apex:outputPanel> 
   </apex:pageBlockSectionItem>     
       
   <apex:pageBlockSectionItem id="countrytitle">    
   <apex:outputLabel value="Zip" />
   <apex:outputPanel layout="block" styleClass="requiredInput">
   <apex:outputPanel layout="block" styleClass="requiredBlock" /> 
        <apex:inputField value="{!newmember.Country__c}"   required="false" id="country"/>  </apex:outputPanel>
   </apex:pageBlockSectionItem>      
       
    <apex:pageBlockSectionItem id="companytitle">    
   <apex:outputLabel value="Company URL" />
   <apex:outputPanel layout="block" styleClass="requiredInput">
   <apex:outputPanel layout="block" styleClass="requiredBlock" /> 
        <apex:inputField value="{!newmember.Company_Url__c}"   required="false" id="company"/>  </apex:outputPanel>
    </apex:pageBlockSectionItem>     
       
       <apex:pageBlockSectionItem id="industrytitle">    
   <apex:outputLabel value="Industry" />
   <apex:outputPanel layout="block" styleClass="requiredInput">
   <apex:outputPanel layout="block" styleClass="requiredBlock" />   
      <apex:inputField value="{!newmember.Industry__c}"   required="false" id="industry"/>  </apex:outputPanel> 
    </apex:pageBlockSectionItem>          
            
   </apex:pageBlockSection>
   
   <apex:pageBlockSection id="mainSection" title="Product & Service Selection"> 

                  <apex:actionRegion >                         
                     <apex:selectList value="{!selectedValue}" multiselect="false" size="1" id="Prodselection">    
                          
                       <apex:selectOptions value="{!someList}"/> 
                       <apex:actionSupport event="onchange" action="{!updateData}" />
                     </apex:selectList>
                  </apex:actionRegion>
</apex:pageBlockSection>

              <apex:pageBlockSection id="section1" rendered="{!section1}">
               <apex:pageBlockSectionItem id="resaleproduct">    
                         <apex:outputLabel value="Product" />
                         <apex:outputPanel layout="block" styleClass="requiredInput">
                          <apex:outputPanel layout="block" styleClass="requiredBlock" />
               <apex:inputField value="{!newmember.Cloud_Product__c}" required="false"/>
                </apex:outputPanel> 
    </apex:pageBlockSectionItem>          
    
     <apex:pageBlockSectionItem id="serviceterm">    
                         <apex:outputLabel value="Service Term" />
                         <apex:outputPanel layout="block" styleClass="requiredInput">
                          <apex:outputPanel layout="block" styleClass="requiredBlock" />    
               <apex:inputField value="{!newmember.Cloud_Service_Term__c}" required="false"/> 
                </apex:outputPanel> 
    </apex:pageBlockSectionItem>  
     
          <apex:pageBlockSectionItem id="quantity">    
                         <apex:outputLabel value="Quantity" />
                         <apex:outputPanel layout="block" styleClass="requiredInput">
                          <apex:outputPanel layout="block" styleClass="requiredBlock" /> 
               <apex:inputField value="{!newmember.Product_Quantity__c}" required="false"/> 
                </apex:outputPanel> 
    </apex:pageBlockSectionItem>
      
                <apex:pageBlockSectionItem id="distributor">    
                         <apex:outputLabel value="Who is Your Meru Authorized Distributor?" />
                         <apex:outputPanel layout="block" styleClass="requiredInput">
                          <apex:outputPanel layout="block" styleClass="requiredBlock" /> 
               <apex:inputField value="{!newmember.Distributor__c}" required="false"/>  
               </apex:outputPanel> 
    </apex:pageBlockSectionItem>
      
       <apex:pageBlockSectionItem id="k12customer">    
                         <apex:outputLabel value="K-12 Customer" />
                         <apex:outputPanel layout="block" styleClass="requiredInput">
                          <apex:outputPanel layout="block" styleClass="requiredBlock" />
               <apex:inputField value="{!newmember.K_12__c}" required="false"/>
                </apex:outputPanel> 
    </apex:pageBlockSectionItem>                 
              </apex:pageBlockSection>

             <apex:pageBlockSection id="section2" rendered="{!section2}">
             <apex:pageBlockSectionItem id="subscriptionterm">    
                         <apex:outputLabel value="Cloud Subscription Term" />
                         <apex:outputPanel layout="block" styleClass="requiredInput">
                          <apex:outputPanel layout="block" styleClass="requiredBlock" />
             <apex:inputField value="{!newmember.Cloud_Subscription_Term__c}" required="false"/>  
             </apex:outputPanel> 
    </apex:pageBlockSectionItem>
    
    <apex:pageBlockSectionItem id="subscriptionQuantity">    
                         <apex:outputLabel value="Quantity" />
                         <apex:outputPanel layout="block" styleClass="requiredInput">
                          <apex:outputPanel layout="block" styleClass="requiredBlock" />
             <apex:inputField value="{!newmember.Product_Quantity__c}" required="false"/>  
              </apex:outputPanel> 
    </apex:pageBlockSectionItem>         
             </apex:pageBlockSection>
             
     
              
   <apex:pageBlockSection title="End Customer Decision Maker" id="EndCustPageBlockSection">
    <apex:pageBlockSectionItem id="firstname">    
                         <apex:outputLabel value="First Name" />
                         <apex:outputPanel layout="block" styleClass="requiredInput">
                          <apex:outputPanel layout="block" styleClass="requiredBlock" />
        <apex:inputField value="{!newmember.First_Name__c}" required="false" id="FirstName"/>
         </apex:outputPanel> 
    </apex:pageBlockSectionItem>   
      <apex:pageBlockSectionItem id="lastname">    
                         <apex:outputLabel value="Last Name" />
                         <apex:outputPanel layout="block" styleClass="requiredInput">
                          <apex:outputPanel layout="block" styleClass="requiredBlock" />
        <apex:inputField value="{!newmember.Last_Name__c}" required="false" id="LastName"/>    
         </apex:outputPanel> 
    </apex:pageBlockSectionItem>    
      <apex:pageBlockSectionItem id="email">    
                         <apex:outputLabel value="Email" />
                         <apex:outputPanel layout="block" styleClass="requiredInput">
                          <apex:outputPanel layout="block" styleClass="requiredBlock" />
        <apex:inputField value="{!newmember.Email__c}" required="false" id="Email"/>
         </apex:outputPanel> 
    </apex:pageBlockSectionItem>   
        <apex:inputField value="{!newmember.Phone__c}" required="false" id="Phone"/>        
   </apex:pageBlockSection>
   
   <apex:pageBlockSection title="Helpful Links">
      <p>
         <a href="http://partners.merunetworks.com/apps/f?p=portal:redirect:::::PGID:38" target="_blank">XPress Cloud Deal Registraiton FAQ</a>
         <br />
         <a href="" target="_blank">XPress Cloud terms and Conditions</a>
      </p>
   </apex:pageBlockSection>
    
       
   <apex:pageBlockSection title="Deal Information" id="DealPageBlockSection">   
    <apex:pageBlockSectionItem id="closedate">    
                         <apex:outputLabel value="Estimated Close Date" />
                         <apex:outputPanel layout="block" styleClass="requiredInput">
                          <apex:outputPanel layout="block" styleClass="requiredBlock" />    
       <apex:inputField value="{!newmember.Estimated_Close_Date__c}" required="false" id="EstimatedClosedate"/>
       </apex:outputPanel> 
    </apex:pageBlockSectionItem>   
    
       <apex:inputField value="{!newmember.Project_Overview__c}" required="false" id="ProjectOverview"/>
       <apex:inputField value="{!newmember.Competitor__c}" required="false" id="Competitor"/>    
       <apex:inputField value="{!newmember.Will_you_manage_the_customers_AP_Network__c}" required="false" id="ManageAPNET"/>             
   </apex:pageBlockSection>

      
                

        
       </apex:pageblock>
      </apex:form>
     </apex:page>

Thanks
Sudhir
Srini NandhiSrini Nandhi
the Fields shouldnot be mandatory on the Object and Should not be use Required on Page.
sudhirn@merunetworks.comsudhirn@merunetworks.com
Thanks You I removed it worked