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
pallipalli 

How to write a server side validations in this page

hi friends,

 please help me ..............

how to write a server side validations on this below page............

i dnt know how to write serverside validations.........

 

 

 

apex page..............

============================

 

<apex:page controller="leadcontroller" id="pg">
<script type="text/javascript">
function formValidator()
{
var fname=document.getElementById("{!$Component.fname}");
if(fname==null || fname=="")
{
alert('Please Enter your First name');
return false;
}
return (true);
}
function formValidator1()
{
var lname=document.getElementById("{!$Component.lname}");
if(lname==null || lname=="")
{
alert('Please Enter your Last name');
return false;
}
return (true);
}
function validatePhone()
{
var phone=document.getElementById("{!$Component.phone}");
if(phone==null || phone=="")
{
alert('You must enter Phone number');
return false;
}
return true;
}
function formValidateEmail()
{
var email=document.getElementById("{!$Component.email}");
if(email==null || email=="")
{
alert('Please Enter a valid Email address');
return false;
}
return true;
}
function validateCompany()
{
var cmpny=document.getElementById("{!$Component.cmpny}");
if(cmpny==null || cmpny=="")
{
alert('Enter your Company name');
return false;
}
return true;
}
</script>
<apex:form id="myform">
<apex:pageBlock id="pb">
<apex:pageBlockSection title="Lead Information" id="pbs" >
<apex:pageBlockSectionItem id="pbsifname">
<apex:outputLabel value="First Name" for="fname"/>
<apex:inputtext value="{!FirstName}" id="fname" onclick="formValidator()"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem id="pbsilname">
<apex:outputLabel value="Last Name" for="lname"/>
<apex:inputtext value="{!LastName}" id="lname" required="true" onclick="formValidator1()"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem id="pbsip">
<apex:outputLabel value="Phone" for="phone"/>
<apex:inputtext value="{!phone}" id="phone" onclick="validatePhone()"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem id="pbsie">
<apex:outputLabel value="Email" for="email"/>
<apex:inputtext value="{!Email}" id="email" onclick="formValidateEmail()"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem id="pbsic">
<apex:outputLabel value="Company" for="cmpny"/>
<apex:inputText value="{!Company}" id="cmpny" required="true" onclick="validateCompany()"/>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
<apex:pageBlockButtons id="pbb">
<apex:commandButton action="{!save}" value="save"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>

 

 

apex class

=================

 

public class leadcontroller
{
public string FirstName{get;set;}
public string LastName{get;set;}
public string phone{get;set;}
public string email{get;set;}
public string Company{get;set;}
public pageReference save()
{
Lead le=new Lead();
le.FirstName=FirstName;
le.LastName=LastName;
le.phone=phone;
le.email=email;
le.company=company;
insert le;

pagereference page = new pagereference('/'+le.id);
return page;

}

}

Hengky IlawanHengky Ilawan

Hi,

 

Javascript runs on client side actually.

If you need to have a server side validation, you can write the validation in your save() methods, and output the error using Message class. Take a look at example here: 

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_pages_message.htm

 

Anyway, if you just need to make a field mandatory, simply add the attribute "required=true" to your apex:input tag.

 

<apex:inputtext value="{!FirstName}" id="fname" required="true"/>

 

Regards,

Hengky

pallipalli

Hi,

 

how to write a server side validations in  firstname and email address ...........

 

i want  code , 

 

i dnot know the how to to write apex class in validations ........

 

please help me ........