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
Dilip vDilip v 

Any sample code for field validation(Vsualforce page)?

I wrote a visual force page and custom controller for simple addition and subtraction.In the page i wrote some simple java script code for field validation but it wasn't work. Any sample code for field validation(Vsualforce page)?
KaranrajKaranraj
Hi Dilip - What type of field validation you are looking for? Can you share your sample code, so that we can look into that and provide you an necessary solution. I suggest you to take look into the the visualforce developer guide to know about how to use java script in visualforce page. http://​http://www.salesforce.com/us/developer/docs/pages/index_Left.htm#StartTopic=Content/pages_javascript_intro.htm

Thanks,
Karan
Dilip vDilip v
Page:
<apex:page controller="Arithmatic_opp">
<apex:form id="firstpage">
    <apex:pageBlock title="Calculator">
        <apex:pageBlockSection collapsible="0">
            <apex:pageblocksectionitem >
                <apex:outputlabel > Enter A value</apex:outputlabel>
                <apex:inputText value="{!A_value}" id="a1"/>
            </apex:pageblocksectionitem>

            <apex:pageBlockSectionItem >
                <apex:outputlabel > Enter B value</apex:outputlabel>
                <apex:inputText value="{!B_value}" id="b1"/>
            </apex:pageBlockSectionItem>
 
        <apex:commandButton value="Addition"   onclick="valid()"/>
        <apex:outputlabel id="one">  {!operation} {!result}</apex:outputlabel>
<script>
 
     function valid()
    {
    var na1=document.getElementById('{!$Component.firstpage.a1}');
    var na2=document.getElementById('{!$Component.firstpage.b1}');
        if(na1==" "&&na2==" ")
        {
        alert("Enter A and B values");
        }
        if(na1==" ")
        {
        alert("please ente A value ");
 
        }
        var na2=document.s.pass.value;
        if(na2==" ")
        {
        alert("please ente B value");
 
        }
     }
 </script>
        <apex:commandButton value="Subtraction"   action="{!subb}"/>
        <apex:outputlabel id="Two"> {!operation1}  {!result}</apex:outputlabel>
    </apex:pageBlockSection>
</apex:pageBlock>
</apex:form>   
</apex:page>

Controller:
public class Arithmatic_opp
{
public Arithmatic_opp() {
    }
private final lead l;
public Arithmatic_opp(ApexPages.StandardController stdController) {
        this.l = (lead)stdController.getRecord();
    }
public integer A_value{get;set;}
public integer B_value{get;set;}
public integer Result{get;set;}
public string operation{get;set;}
public string operation1{get;set;}
public pagereference subb()
{
result=A_value-B_value;
operation='Result after performing Subtraction operation';
return null;
}
public pagereference Add()
{
result=A_value+B_value;
operation1='Result after performing Addition operation';
return null;
}
}

 
Pritam ShekhawatPritam Shekhawat
hii Dilip,
           Follow this link http://www.cloudforce4u.com/2013/07/show-error-message-in-visualforce.html its help you for some sample code for validation in vf Page.
below is sample code.
<apex:page standardController="Account" extensions="ErrorDisplayDemoController">
 <apex:form >
   <apex:pageblock >
      <apex:pageMessages id="shwmsg"></apex:pageMessages>
         <apex:panelGrid columns="2">
           Account Name <apex:inputtext value="{!accountname}"/>
           Account Number <apex:inputtext value="{!accountnumber}"/>
           <apex:commandButton value="Update" action="{!UpdateAccount}" style="width:70px" rerender="shwmsg"/>
         </apex:panelGrid>
    </apex:pageblock>
 </apex:form>
</apex:page>
Thanx
Pritam Shekhawat
 
Dilip vDilip v
Thnk you Pritam .

 I watched the blog but there he used apex custom controller for the validation.

But i need javascript  code for validating the fields.