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
sagar@sfdcsagar@sfdc 

how to add JavaScript validations for pageblocksectionitems in vfpage

hi,

 

vl javascript work on pageblocksectionitems , i added validations for it, but it is not working ,...dis my code , ican u plz provide solution for this 

 

<apex:pageBlockSection title="Nonprofit Organization"
columns="2" id="pbset3">
<apex:pageBlockSectionItem >
<Label>Organization's Name</Label>
<apex:inputfield styleclass="textboxred" id="orgName"
onkeyup="textLimit('{!$Component.OrgName}',80);"
style="width:200px;" value="{!Actinterest.Name}" />
</apex:pageBlockSectionItem>

</apex:pageBlockSection>

 <apex:commandButton styleclass="submit" value="Submit" onclick="return checkvalidity('{!$Component.orgName}');"

 action="{!save}" />

 

 

 

 

<script language="javascript" type="text/javascript">
function checkvalidity()
{

if(document.getElementById("{!$Component.orgName}").value == ' ')
{
alert('please enter organization name');
return false;
}

else{
return true;

}
}
</script>


Navatar_DbSupNavatar_DbSup

Hi,

On the click event of submit button you are calling the “checkvalidity” function and passing the argument.

But in your JavaScript “checkvalidity” function definition is different (function without parameter. If you want to call the “checkvalidity” function remove {!$Component.orgName}

Try the below code snippet as reference:

<apex:pageBlockSection title="Nonprofit Organization"
columns="2" id="pbset3">
<apex:pageBlockSectionItem >
<Label>Organization's Name</Label>
<apex:inputfield styleclass="textboxred" id="orgName"
onkeyup="textLimit('{!$Component.OrgName}',80);"
style="width:200px;" value="{!Actinterest.Name}" />
</apex:pageBlockSectionItem>

</apex:pageBlockSection>

 <apex:commandButton styleclass="submit" value="Submit" onclick="return checkvalidity();"

 action="{!save}" />

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

 

sagar@sfdcsagar@sfdc

hi ,

 

i removed parameters and i mentioned .onclick="return checkvalidity();" even though it is not working , and i tryied by providing the ids of page,form,and pageblock   in javascript as

 

if(document.getElementById("{!$Component.page1.form1.pb1.orgName}").value == '')
{
alert('please enter organization name');
return false;
}

harshasfdcharshasfdc

Hi sagar,

 

write the if condition in controller and error message in

ApexPages.addMessage(new ApexPages.Message ApexPages.Severity.ERROR, ex.getDmlMessage(i)))

and in vf page write 

<apex:messages></apex:messages>

 

Thanks,

Harsha

sagar@sfdcsagar@sfdc

thanks harsha 

 

i completed in client side only by javascript ,