+ Start a Discussion
CoderCoder 

How to validate two InputText fields in visualforce page?

Hi,

 

I created a two input fields for Fathers name and Guardians Name in visual force page, in this i want to make either one field value is required. For example here user can enter either Fathers Name or Guardians Name or else he can enter values in both fields.Here i tried if condition for checking but i'm not getting the output.Can any one help me to solve this.

 

My Visualforce Page code:

 

<apex:page controller="prac"> <apex:pageBlock > <apex:form > <p><apex:outputLabel value="Father"/></p> <apex:inputText value="{!Father}" id="fName"/> <p><apex:outputLabel value="Guardian"/></p> <apex:inputText value="{!Guardian}"/> <p><apex:commandButton value="Save" action="{!mySave}"/></p> </apex:form> </apex:pageBlock> </apex:page>

My Apex Class Code:

 

public class prac { string fname; string gname; public string getFather() { return fname; } public void setFather(string s) { this.fname=s; } public string getGuardian() { return gname; } public void setGuardian(String s){ this.gname=s; } public pageReference mySave() { pageReference newPage; if(fname == null) { newPage=Page.currentPage; } newPage=page.NextPage; return newPage; } }

 

Best Answer chosen by Admin (Salesforce Developers) 
metaforcemetaforce
This should be a simple one.

<apex:page controller="clssfpoc">
<apex:pageBlock >
<apex:form >
<p>
<apex:outputLabel value="Father"/>
</p>
<apex:inputText value="{!Father}" id="fName" />

<p>
<apex:outputLabel value="Guardian"/>
</p>
<apex:inputText value="{!Guardian}" id="gName" />

<p>
<apex:commandButton value="Save" onclick="return validate()" />
</p>
<script language="javascript">
function validate()
{
try
{
var fNameObj = document.getElementById("{!$Component.fName}");
var gNameObj = document.getElementById("{!$Component.gName}");
if(trim(fNameObj.value) == "" && trim(gNameObj.value) == "")
{
alert("Father/Guardian is mandatory!");
return false;
}
return true;
}
catch(e)
{
alert(e);
return false;
}
}
</script>
</apex:form>
</apex:pageBlock>
</apex:page>

 

AD

-----------------------------------------------------------------------------

Mark it as an accepted solution if it works and help the community

All Answers

metaforcemetaforce

In your code, the control will always go the NextPage as this statement executes unconditionally:

 

newPage=page.NextPage; 

 

So although you set the newPage as page.currentPage in the "if" block, it gets overwritten with the above statement always, so you can put it in else block and/or improve your conditional logic otherwise.  

 

Alternatively, you can do the following to implement your business rule:

 

1. Apply client side validation in JavaScript which checks that at least one field is not blank. But this will take care of only that scenario when the data is being submitted through the page. If records are being inserted from any other source, e.g. data loader, this check will not run.

 

2. Write a server side validation rule to check for this condition and display an error message in your

<apex: pageMessages> included in your VF page. This will take care of all the scenarios where record is being inserted from any source, be it VF page, or data loader.

 

AD

--------------------------------------------------------------------------

Mark it as an accepted solution if it works and help the community

CoderCoder
will you please post the sample javascript code.
metaforcemetaforce
This should be a simple one.

<apex:page controller="clssfpoc">
<apex:pageBlock >
<apex:form >
<p>
<apex:outputLabel value="Father"/>
</p>
<apex:inputText value="{!Father}" id="fName" />

<p>
<apex:outputLabel value="Guardian"/>
</p>
<apex:inputText value="{!Guardian}" id="gName" />

<p>
<apex:commandButton value="Save" onclick="return validate()" />
</p>
<script language="javascript">
function validate()
{
try
{
var fNameObj = document.getElementById("{!$Component.fName}");
var gNameObj = document.getElementById("{!$Component.gName}");
if(trim(fNameObj.value) == "" && trim(gNameObj.value) == "")
{
alert("Father/Guardian is mandatory!");
return false;
}
return true;
}
catch(e)
{
alert(e);
return false;
}
}
</script>
</apex:form>
</apex:pageBlock>
</apex:page>

 

AD

-----------------------------------------------------------------------------

Mark it as an accepted solution if it works and help the community

This was selected as the best answer
CoderCoder
Hi Thank you very much......I got output.......