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 script validaions in visualforce page

hi, how to write script validations for  Firstname, email,phone,.............

 

 

please help me

 

<apex:page controller="leadcontroller">
<script>

</script>
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection >
<apex:pageBlockSectionItem >
<apex:outputLabel value="First Name" for="fname"/>
<apex:inputtext value="{!FirstName}" id="fname"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel value="Last Name" for="lname"/>
<apex:inputtext value="{!LastName}" id="lname"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel value="Phone" for="phone"/>
<apex:inputtext value="{!phone}" id="phone"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel value="Email" for="email"/>
<apex:inputtext value="{!Email}" id="email"/>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
<apex:pageBlockButtons >
<apex:commandButton action="{!save}" value="save"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>

Saurabh DhobleSaurabh Dhoble

I assume you want to do a "Client-side" validation - validate a page before it is posted to the server. This requires javascript to be added to the page. Here's a sample code I wrote to make sure the the "First Name" field has a value in it.

Of special note is the way Salesforce changes the IDs of objects - you cannot refer to an element by just its ID defined in your visualforce page. Salesforce changes the object's ID to something like [Form Id]:[page Block Id]:[page block section id]:[object id].

 

You can run this in a standalone fashion, and tweak the code if you wish to try out different things :-

 

Here's the visualforce page :-

 

<apex:page standardController="Account" extensions="Validate_Account_Class">
    <script>
            function validateFName()
            {
            	var fname = document.getElementById('j_id0:myForm:myPageBlock:myPageBlockSection:myFirstName');
            	
            	if(fname==null)
            	{
            		alert("Unable to find the textbox");
            		return false;
            	}
            	else
        		{
        			if(fname.value == "")
        			{
        				alert("Please enter a value for First Name");
        				return false;
        			}
				}
            	return true;
            }
	</script>
    <apex:form id="myForm">
        <apex:pageBlock id="myPageBlock">
        	<apex:pageBlockSection id="myPageBlockSection">
            	<apex:outputLabel value="First Name"></apex:outputLabel>
                <apex:inputText id="myFirstName" value="{!FName}" />
            </apex:pageBlockSection>
            <apex:commandButton id="submitPage" onclick="validateFName();" value="Save" /> 
        </apex:pageBlock>
	</apex:form>
</apex:page>

 

And here's the Apex class :-

 

public class Validate_Account_Class {
	Account a;
    public Validate_Account_Class(ApexPages.StandardController controller)
    {
        a = (Account) controller.getRecord();
    }
    
    public String FName
    {
        get
        {
            if(FName == null)
                return '';
            return FName;
        }
        set;
    }
    
    public String Email
    {
        get
        {
            if(Email == null)
                Email = 'test@test.com';
            return Email;
        }
        set;
    }
}