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
Ravi kumar 292Ravi kumar 292 

How to make Field mandatory in VF Page

Hi all,

I have a scenario, i am using <apex:outputText> to display the custom object fields on a vf page like, FirstName, LastName, DOB, Mobile etc..
My requirement is, i want to display the mandatory field on the vf page.

For example FirstName* or Firstname|(red vertical bar).

Please help on this..

Thanks in advance.

 
Best Answer chosen by Ravi kumar 292
Vignesh P 6Vignesh P 6
Hi Ravi,

This code may help you.
<apex:page>
    <apex:form>
        <apex:pageBlock >
            <apex:outputPanel>
                <div class="requiredInput">
                <div class="requiredBlock"></div>
                <apex:outputText value="This is the required output text field" />
                </div>
            </apex:outputPanel>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Thanks,
Vignesh

 

All Answers

Vignesh P 6Vignesh P 6
Hi Ravi,

This code may help you.
<apex:page>
    <apex:form>
        <apex:pageBlock >
            <apex:outputPanel>
                <div class="requiredInput">
                <div class="requiredBlock"></div>
                <apex:outputText value="This is the required output text field" />
                </div>
            </apex:outputPanel>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Thanks,
Vignesh

 
This was selected as the best answer
JyothsnaJyothsna (Salesforce Developers) 
Hi Ravi,
 Please check the below sample code
<apex:page standardController="contact">
 <apex:form >

<apex:messages />

<apex:pageBlock title="PageBlock" mode="edit">

<apex:pageBlockSection title="Page block section">

  <apex:outputLabel value="Birth date" for="mId"/>

  <apex:outputPanel styleClass="requiredInput" layout="block">

   <apex:outputPanel styleClass="requiredBlock" layout="block"/>

  <apex:inputText value="{!contact.Birthdate}" required="true" id="mId"/>

 </apex:outputPanel>

</apex:pageBlockSection>

<apex:pageblockButtons >

<apex:commandButton action="{!save}" value="save"></apex:commandButton>

</apex:pageblockButtons>

</apex:pageBlock>

</apex:form>
</apex:page>

User-added image


Hope this helps you!
Best Regards,
Jyothsna
ShivaKrishna(Freelancer)ShivaKrishna(Freelancer)
Hi Ravi,

Fist thing you can not use <apex:outputText> for mandatory field as you can not enter any value for these fields

these are the ways you can do 
use <apex:inputField value="{!fieldname}"> and make that field as mandatory on field level
use  <apex:inputField value="{!fieldname}" required ="true">
use <apex:inputText value="{!fieldname}" required = "true">

in all the cases you will still red line against field as you required.

let me know, if it helps you or need help :)
shiva.sfdc.backup@gmail.com
Ravi kumar 292Ravi kumar 292
Thanks Vignesh.. 
gopal r 11gopal r 11
thank you ,shiva krishna it's working for inputfield lebel only, not for input text.can you elaborate for input text value also