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
Nick KeehanNick Keehan 

Visualforce - Require on or the other field to be entered

Hi guys.

Looking for a piece of code to allow me to Validate that one or the other field has a value inside. These are custom fields in VF so i cant create a object validation rule as these are just input text fields.

e.g. Field 1 or Field 2 must be mandatory.

Using a standard controller and Ext Controller. 

Any help would be great.

Nick

 
Best Answer chosen by Nick Keehan
Tarun SuriTarun Suri
Hi Nick,

If You want to make both fields mandatory You can simply do
<apex:inputtext value="{!Field1}"  required="true"/>
<apex:inputtext value="{!Field2}"  required="true"/>
If You want to make either field1 or field 2 mandatory You have to touch the controller.
In controller u need to check 
if ( (Field1 != NULL && Field2 == NULL) || (Field2 != NULL && Field1 == NULL)  || (Field1 != NULL && Field2 |= NULL) )
{
        //Do your coding here
}

Hope it solves Your Problem.
Thanx
Tarun Suri