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
sankumsankum 

validation on vf page using java script

hi,

this is kiran i am developing vf page i have two fields like firstname and last name,

my condition is first name and last name are not same , if both fields are same display the error message.

i tried following code but it doesn't  work  

 

<apex:page>
<apex:form >
<apex:pageblock >
<apex:pageBlockSection >
First name:
<apex:inputtext value="{!name}" id="name2"/><br/>
Last Name:
<apex:inputText value="{!name1}" id="name3"/><br/>
<apex:commandButton onclick=" return validate" value="submit"/>
<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:pageBlockSection>
</apex:pageblock>

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

 

prakash_sfdcprakash_sfdc
<apex:page>
<apex:form >
<apex:pageblock >
<apex:pageBlockSection >
First name:
<apex:inputtext value="{!name}" id="name2"/><br/>
Last Name:
<apex:inputText value="{!name1}" id="name3"/><br/>
<apex:commandButton onclick=" return validate();" value="submit"/>
&lt;script language="javascript">
function validate()
{
try
{
var fNameObj = document.getElementById("{!$Component.name2}");
var gNameObj = document.getElementById("{!$Component.name3}");
if(trim(fNameObj.value) == "" && trim(gNameObj.value) == "")
{
alert("Father/Guardian is mandatory!");
return false;
}
return true;
}
catch(e)
{
alert(e);
return false;
}
}
&lt;/script&gt;


</apex:pageBlockSection>
</apex:pageblock>

</apex:form>
</apex:page>
sankumsankum
thanks..

--
Thanks&Regards
KIRAN
prakash_sfdcprakash_sfdc
Please mark it as solution and give kudos, if it helped.