[SalesForce] Make a field required on condition

I am trying to make a field required on condition in vf page as below. But the red color bar is not appearing. How to make the red bar appear.

<apex:inputtext value="{!b.Quantity__c}"  required="{!IF(b.Service__c!=' ',true,false)}"/>

Regards

Best Answer

Try with apex:inputField and use conditionally required attribute

<apex:inputField value="{!b.Quantity__c}"  required="{!IF(b.Service__c!=' ',true,false)}"/>

OR If you still want to go with apex:inputText and want to show red mark then use below code based on required hide/show field

<apex:outputPanel styleClass="requiredInput" layout="block">
   <apex:outputPanel styleClass="requiredBlock" layout="block"/>
  <apex:inputText value="{!contact.Birthdate}" required="true" id="mId"/>
 </apex:outputPanel>