enter image description here

how to validate the fields used in a visualforce page?

2 Answers 2


You can use the Required attribute of the inputfield vf tag

<apex:inputfield required="true" value="" />
  • Thanks for your answer,but i need to display first name is required,last name is required like this name need to mention and need to show at a time to show 2 errors when 2 fields is empty.when 1 field is empty need to show one error.
    – user
    Feb 1, 2016 at 11:09

you can show by custom way Page

<apex:outputPanel id="showError" layout="blank" rendered="{!if(strCustomErrorMsg != '',true,false)}">
   <apex:outputText value="{!strCustomErrorMsg}" />
<apex:inputText value="{!objAcc.FirstName}"/>
<apex:inputText value="{!objAcc.LastName}"/>
 <apex:commandbutton action="{!Save}" rerender="showError"/>


public string strCustomErrorMsg {get;set;}

public void save(){
  strCustomErrorMsg = '';
  if(objAcc.FirstName == null || objAcc.FirstName == '')
    strCustomErrorMsg = 'First Name required';
  if(objAcc.FirstName == null || objAcc.FirstName == '')
    strCustomErrorMsg = strCustomErrorMsg + '<br/> Last Name required';

I have not tested this code, hope this will help you..

  • its not working.In save method i need to use pagereference.while all fields had given it need to move another page....
    – user
    Feb 1, 2016 at 12:00
  • you can use common controller for the both the pages
    – apKash
    Feb 1, 2016 at 13:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.