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 

How can i use javascript validation in apex code development, plz ............

GomsueyGomsuey

What sort of validation are you looking forward??

I mean if you want to apply validation on a textbox then

 

 

<apex:page id='pg1'>
<apex:form id='frm1'>
<apex:inputtext id="idInputTxt" value="{!usrName}"/>
<apex:commandButton value="Save" onClick="funcRedirect();"/>

<script>
function funcRedirect()
{
 if(document.getElementById("{!$Component.pg1.frm1.idInputTxt}").value == 'Gomsuey')
 {
  alert('USERNAME CORRECT');
 }
 else
 {
  alert('USERNAME INCORRECT');
 }
}
</script>
</apex:form>
</apex:page>

Hope its helpful !!!

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 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>
GomsueyGomsuey

Hey Check this out : 

 

<apex:page id="ats"  tabStyle="Opportunity">
  <apex:form id="firstpage">
    <apex:outputLabel value="Residence"></apex:outputLabel >
  <apex:inputText id="Residency"/>
  <apex:commandButton value="Validate" onclick="validatephone();" />

  <script>
  function validatephone()
  {
      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

 

You have two options that are quite elegant,

 

 

  1. Masking your input.
  2. Using jQuery for validation.

 

I'd recommend the second option, it's neat and easy to implement.

 

Cheers,

Wes 

mohimohi
<script>
function validateresedency()
{
var av=document.getElementById("{!$Component.ats.firstpage.pageBlock1.pageblockSection2.Residency}").value;
alert(av);
if(av=="")
{
alert('Plz Enter a valid Number');
}
}
</script>
dev_smiledev_smile

<apex:page>  

<apex:form>

<apex:inputText id="totArea" value="{!objSubFacility.subfacility.TotalArea__c}" onchange="funcRedirect(this);" />


<script>
function funcRedirect(id1){
var nameRegxp1 = /^([0-9_.]+)$/;
     var val=document.getElementById(id1.id).value;
     if (nameRegxp1.test(val) != true){
          alert("Enter valid number");
          document.getElementById(id1.id).value='';
        document.getElementById(id1.id).focus(); 
        return false;    
     }
     return true; }
</script>

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

 

try this

dev_smiledev_smile

<apex:inputText id="totArea" value="hello" onchange="funcRedirect(this);" />

small correction