+ 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)?
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

Dilip vDilip v
<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:outputlabel > Enter B value</apex:outputlabel>
                <apex:inputText value="{!B_value}" id="b1"/>
        <apex:commandButton value="Addition"   onclick="valid()"/>
        <apex:outputlabel id="one">  {!operation} {!result}</apex:outputlabel>
     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");
        <apex:commandButton value="Subtraction"   action="{!subb}"/>
        <apex:outputlabel id="Two"> {!operation1}  {!result}</apex:outputlabel>

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()
operation='Result after performing Subtraction operation';
return null;
public pagereference Add()
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"/>
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.