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

scroll back to error messages in VF page

HI All,

I have created a visualforce page which contains many inputfileds to accept values from user. i'm showing some visualforce error messages on top of the page, if user forgets to enter values in inputfields. 
what i want here is whenever user enters some values and clicked on submit button,if it shows any errors it should automatically go back to the the place where error messages are displyed.(i.e we need to set focus on Error messages.)
I hope my question is clear.
Any suggestions or code snippet is appriciated.

Amit Chaudhary 8Amit Chaudhary 8
Please check below post. I hope that will help you
<apex:page standardController="Account" extensions="ErrorMessageInVfController">
 <apex:form >
   <apex:pageblock >
      <apex:pageMessages id="showmsg"></apex:pageMessages>
         <apex:panelGrid columns="2">
           Account Name: <apex:inputText value="{!}"/>
           Account Number: <apex:inputText value="{!acc.AccountNumber}"/>
           Account Phone: <apex:inputText value="{!}"/>
           Account Site: <apex:inputText value="{!}"/>
           Account Industry: <apex:inputText value="{!acc.industry}"/>
           <apex:commandButton value="Update" action="{!save}" style="width:90px" rerender="showmsg"/>
public with sharing class ErrorMessageInVfController {
    public Account acc{get;set;}
    public ErrorMessageInVfController(ApexPages.StandardController controller) {
        acc = new Account();
    public void save(){
      if( == '' || == null)
       ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'Please enter Account name'));
      if(acc.AccountNumber == '' || acc.AccountNumber == null)
       ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please enter Account number'));
      if( == '' || == null)
       ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please enter Account phone'));
      if( == '' || == null)
       ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,'Please enter Account site'));
      if(acc.industry == '' || acc.industry == null)
       ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,'Please enter Account industry'));

Let us k now if this will help you

Amit Chaudhary

Hi amit,

Thanks for your reply.
I think u didnt understand my question. you came to to half in the solution.
so my question is u have several fields in the form say 20.think like there are few mandatory.
we have a button down in the page(bottom of the page.) because u have more fields in the page we will get scroll bar. here comes my question, when u click on that button without giving few mandatory values page will give error messages on the top of the page.but u cant see them until and unless u scroll to top of page.
i want this scrolling to be automatic, that means whenever u get error message in the we should automatically go back to the place where error msgs are displyed.
i hope my question is clear now.