function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Vigneshwaran LoganathanVigneshwaran Loganathan 

Javascript to check field is empty

Hi, I have to check whether the field is empty before rendering that to another page. if its empty, IT SHOULD NOT go to next page. I have used Javascript for that.  But its not Triggering.
 
<apex:page standardController="SME_Coaching__c" extensions="smecoachingcontroller" id="page" >
<apex:sectionHeader title="SME Coaching" subtitle="Coaching Schedule" help="https://www.salesforce.com"/>
 <apex:form id="f1" >
 
 
<script>
    function show()
    {
        var name=document.getElementById('page:f1:p1:ip1').value;
        if(name== "" || name==null)
        {
            document.getElementById("page:f1:p1:sc:ip1").innerHTML = "SME Name should not be Empty"; 
        }
    }
    </script>

 <apex:pageblock id="p1" >
      <apex:pageBlockButtons >   
         <apex:commandButton value="Check Availability" disabled="{!checkbutton}" action="{!availability}" onclick="return show();"/>
       </apex:pageBlockButtons>
      <apex:Pageblocksection id="sc" title="Skills and Tools" rendered="{!skillfield}">
            <apex:inputField value="{!SME_Coaching__c.Skills_Catagory__c}" required="true" id="sy" />
              <apex:inputfield value="{!SME_Coaching__c.sme1__c}" required="true" id="ip1"/>
      </apex:Pageblocksection>

 
Vishal_GuptaVishal_Gupta
Hi,

You need to return false in your javascript function, please use below code :
<script>
    function show()
    {
        var name=document.getElementById('page:f1:p1:ip1').value;
        if(name== "" || name==null)
        {
            document.getElementById("page:f1:p1:sc:ip1").innerHTML = "SME Name should not be Empty";
             return false;
        }
return true;
    }
    </script>
Please let me know if it will work for you.

Thanks,
Vishal