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
mohimohi 

javascript in apex

<apex:page id="ats" controller="opportunityController" tabStyle="Opportunity">
  <!-- Begin Default Content REMOVE THIS -->
  <apex:sectionHeader title="new Customer Opportunity" subtitle="Step 1 to 3"/>
 
  <h1>Congratulations</h1>
  <apex:form id="firstpage">
  <apex:pageBlock title="Customers Information">
  <apex:facet name="footer">
  <apex:outputPanel >
  <apex:commandButton action="{!Step2}" value="next"  styleClass="btn" />
  <apex:commandButton action="{!save}" value="save" onclick="validatephone();"  styleClass="btn" />
  </apex:outputPanel>
  </apex:facet>
  <apex:pageblockSection title="Account Information">
  <apex:panelGrid columns="2">
  <apex:outputLabel value="Account Name" for="accountName"></apex:outputLabel>
  <apex:inputField id="accountName" value="{!account.name}"/>
  <apex:outputLabel value="site" for="accountSite"></apex:outputLabel>
  <apex:inputField id="accountSite" value="{!account.site}"/>
  </apex:panelGrid>
  </apex:pageblockSection>
  <apex:pageblockSection title="Contact Information">
  <apex:panelGrid columns="2">
  <apex:outputLabel value="First Name" for="contactFirstName"></apex:outputLabel>
  <apex:inputField id="contactFirstName" required="true" value="{!contact.firstName}"/>
  <apex:outputLabel value="Last Name"  for="contactLastName"></apex:outputLabel>
  <apex:inputField id="contactLastName" value="{!contact.lastName}"/>
  <apex:outputLabel value="Phone" for="contactPhone" ></apex:outputLabel>
  <apex:inputField id="contactPhone" required="true" value="{!contact.Phone}"/>
  <apex:outputLabel value="Residence"></apex:outputLabel>
  <apex:inputText id="Residency" />
  </apex:panelGrid>
  </apex:pageblockSection>
 
  </apex:pageBlock>
  </apex:form>
  <script>
  function validatephone()
  {
  var av=document.getElementById("{!$Component.firstpage.Residency}"});
  if(av=="")
  {
  alert("Plz Enter a valid Number")
  }
  }
  </script>
  This is your new Page
 
</apex:page>

in this code java script is not working i m a trainee on this plz help me

bob_buzzardbob_buzzard

Can you post your code using the Insert code button on the editor - (clipboard with a c inside it)?

 

Otherwise we can't see exactly what's going on with brackets and colons etc - they turn into smileys.

mohimohi

 simply call the  javascript  function on button click,

 

mohimohi
<apex:page id="ats" controller="opportunityController" tabStyle="Opportunity">
  <!-- Begin Default Content REMOVE THIS -->
  <apex:sectionHeader title="new Customer Opportunity" subtitle="Step 1 to 3"/>
 
  <h1>Congratulations</h1>
  <apex:form id="firstpage">
  <apex:pageBlock title="Customers Information">
  <apex:facet name="footer">
  <apex:outputPanel >
  <apex:commandButton action="{!Step2}" value="next"  styleClass="btn" />
  <apex:commandButton value="save" onclick="validateresedency();"  />
  </apex:outputPanel>
  </apex:facet>
  <apex:pageblockSection title="Account Information">
  <apex:panelGrid columns="2">
  <apex:outputLabel value="Account Name" for="accountName"></apex:outputLabel>
  <apex:inputField id="accountName" value="{!account.name}"/>
  <apex:outputLabel value="site" for="accountSite"></apex:outputLabel>
  <apex:inputField id="accountSite" value="{!account.site}"/>
  </apex:panelGrid>
  </apex:pageblockSection>
  <apex:pageblockSection title="Contact Information">
  <apex:panelGrid columns="2">
  <apex:outputLabel value="First Name" for="contactFirstName"></apex:outputLabel>
  <apex:inputField id="contactFirstName" required="true" value="{!contact.firstName}"/>
  <apex:outputLabel value="Last Name"  for="contactLastName"></apex:outputLabel>
  <apex:inputField id="contactLastName" value="{!contact.lastName}"/>
  <apex:outputLabel value="Phone" for="contactPhone" ></apex:outputLabel>
  <apex:inputField id="contactPhone" required="true" value="{!contact.Phone}"/>
  <apex:outputLabel value="Residence"></apex:outputLabel>
  <apex:inputText id="Residency" />
  </apex:panelGrid>
  </apex:pageblockSection>
 
  </apex:pageBlock>
  <script type="text/javascript">
  function validateresedency()
  {
  var av=document.getElementById("{!$Component.ats.firstpage.Residency}"}.value);
  if(av=="")
  {
  alert('Plz Enter a valid resedency');
  }
  }
  </script>
  </apex:form>
 
  This is your new Page
 
</apex:page>
mohimohi
<apex:page id="ats" controller="opportunityController" tabStyle="Opportunity">
 
  <apex:sectionHeader title="new Customer Opportunity" subtitle="Step 1 to 3"/>
  <h1>Congratulations</h1>
  <apex:form id="firstpage">
  <apex:pageBlock title="Customers Information">
  <apex:facet name="footer">
  <apex:outputPanel >
  <apex:commandButton action="{!Step2}" value="next"  styleClass="btn" />
  <apex:commandButton value="ChkValidate" onclick="validateresedency();"  />
  </apex:outputPanel>
  </apex:facet>
  <apex:pageblockSection title="Account Information">
  <apex:panelGrid columns="2">
  <apex:outputLabel value="Account Name" for="accountName"></apex:outputLabel>
  <apex:inputField id="accountName" value="{!account.name}"/>
  <apex:outputLabel value="site" for="accountSite"></apex:outputLabel>
  <apex:inputField id="accountSite" value="{!account.site}"/>
  </apex:panelGrid>
  </apex:pageblockSection>
  <apex:pageblockSection title="Contact Information">
  <apex:panelGrid columns="2">
  <apex:outputLabel value="First Name" for="contactFirstName"></apex:outputLabel>
  <apex:inputField id="contactFirstName" required="true" value="{!contact.firstName}"/>
  <apex:outputLabel value="Last Name"  for="contactLastName"></apex:outputLabel>
  <apex:inputField id="contactLastName" value="{!contact.lastName}"/>
  <apex:outputLabel value="Phone" for="contactPhone" ></apex:outputLabel>
  <apex:inputField id="contactPhone" required="true" value="{!contact.Phone}"/>
  <apex:outputLabel value="Residence"></apex:outputLabel>
  <apex:inputText id="Residency"  />
  </apex:panelGrid>
  </apex:pageblockSection>
  </apex:pageBlock>
  <script>
  function validateresedency()
  {
      var av=document.getElementById("{!$Component.ats.firstpage.Residency}").value;
      if(av=="")
      {
          alert('Plz Enter a valid Number');
      }
  }
 </script>
 </apex:form>
 
 
 
</apex:page>
WesNolte__cWesNolte__c

Hey

 

The reason is that there are some weird rules around $Component and as a result {!$Component.ats.firstpage.Residency} is not going to return the value you expect. There's an article that details the solution here.

 

Cheers,

Wes 

mohimohi

Dear Sir

 

i m starter with the salesforce.

your above solution is not working.

what i want to do is that there is a inputtext field,

i want to apply the the required field validation

on that . plz help i have given u the code 

Thanks

 

 

WesNolte__cWesNolte__c

Try this, 

 

<apex:smileytongue:age id="ats" controller="opportunityController" tabStyle="Opportunity">
  
  <apex:sectionHeader title="new Customer Opportunity" subtitle="Step 1 to 3"/>
  <h1>Congratulations</h1>
  <apex:form id="firstpage">
  <apex:smileytongue:ageBlock title="Customers Information">
  <apex:facet name="footer">
  <apex:smileysurprised:utputPanel >
  <apex:commandButton action="{!Step2}" value="next"  styleClass="btn" />
  <apex:commandButton value="ChkValidate" oncomplete="validateresedency();"  />
  </apex:smileysurprised:utputPanel>
  </apex:facet>
  <apex:smileytongue:ageblockSection title="Account Information">
  <apex:smileytongue:anelGrid columns="2">
  <apex:smileysurprised:utputLabel value="Account Name" for="accountName"></apex:smileysurprised:utputLabel>
  <apex:inputField id="accountName" value="{!account.name}"/>
  <apex:smileysurprised:utputLabel value="site" for="accountSite"></apex:smileysurprised:utputLabel>
  <apex:inputField id="accountSite" value="{!account.site}"/>
  </apex:smileytongue:anelGrid>
  </apex:smileytongue:ageblockSection>
  <apex:smileytongue:ageblockSection title="Contact Information">
  <apex:smileytongue:anelGrid columns="2">
  <apex:smileysurprised:utputLabel value="First Name" for="contactFirstName"></apex:smileysurprised:utputLabel>
  <apex:inputField id="contactFirstName" required="true" value="{!contact.firstName}"/>
  <apex:smileysurprised:utputLabel value="Last Name"  for="contactLastName"></apex:smileysurprised:utputLabel>
  <apex:inputField id="contactLastName" value="{!contact.lastName}"/>
  <apex:smileysurprised:utputLabel value="Phone" for="contactPhone" ></apex:smileysurprised:utputLabel>
  <apex:inputField id="contactPhone" required="true" value="{!contact.Phone}"/>
  <apex:smileysurprised:utputLabel value="Residence"></apex:smileysurprised:utputLabel>
  <apex:inputText id="Residency"  />

<script>

 var av=document.getElementById("{!$Component.Residency}").value;

</script>
  </apex:smileytongue:anelGrid>
  </apex:smileytongue:ageblockSection>
  </apex:smileytongue:ageBlock>
  <script>
  function validateresedency()
  {
      
      if(av=="":smileywink:
      {
          alert('Plz Enter a valid Number');
      }
  }
 </script> 
 </apex:form>

dev_smiledev_smile

 function validateresedency()
   {
      var av=document.getElementById('{!$Component.ats.firstpage.Residency}').value;
       if(av==""
       {
    alert('Plz Enter a valid Number');
       }
   }

 

Use the above code to get value page id must also be mentioned.