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
suneel raosuneel rao 

Validations for the check box in custom vfpage

Hi all,
I want to validate the custom check box before submitting Force.com sites custom vfpage. Can we use standard validation over here...? because i want to make the check box as mandetory. how can we achive this. Can anyone help me over here.

Thanks in advance.
regards,
Suneel.
ShikibuShikibu
<apex:pageBlock >
    <apex:pageBlockSection>
        <apex:pageBlockSectionItem>
            <apex:outputPanel>
                <div class="requiredInput">
                    <div class="requiredBlock"></div>
                    <apex:inputText value="{!someApexProperty}"/>
                </div>
            </apex:outputPanel>
        </apex:pageBlockSectionItem>
    </apex:pageBlockSection>
</apex:pageBlock>

Validations will apply even if you are saving via a visualforce controller.

If you want to display an input on a VF page as required (even though the field is not defined as required in the schema), you can do as follows:


 
suneel raosuneel rao
Hi Shikibu,
I using the custom checkbox field in custom vfpage with custom controller. I have tried with div class which you have mentioned. but i still not able to validate the checkbox. can you help how we can validate the check box by using controller.
Sukesh Kumar 33Sukesh Kumar 33
Make use of javascript to validate to check if checkbox is checked
ShikibuShikibu
The bit of markup I showed you only displays the red line; it does no validation.

See here (http://salesforce.stackexchange.com/questions/22571/how-to-write-validation-in-visualforce/22635) for an example of writing validation in VisualForce page controller.
Nachu RY 4Nachu RY 4
Hi,

<apex:page showHeader="false" sidebar="false">
<apex:form >
<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
    $(document).ready(function(){
        $('[id$=btn]').click(function(){
            if($('#chk2').prop('checked') == false){
                alert('Please select the checkbox');
                return false;
            }
        });
    });
</script>
   Do u need ? &nbsp;  <input type="checkbox" id="chk2"/><br/>
   
   
   <apex:commandButton onclick="fn()" id="btn" value="Save"/>
</html>
</apex:form>
</apex:page>

Thanks.
suneel raosuneel rao
Hi Nachu RY 4,
I have tried this but value is saving as false when the record is saved.
Nachu RY 4Nachu RY 4
Hi,

If you are using custom controller .
Hope it will work for you.

Page:
-------------------
<apex:page showHeader="false" sidebar="false" controller="savecontroller">
<apex:form >
<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
    $(document).ready(function(){
        $('[id$=btn]').click(function(){
            if($('#chk2').prop('checked') == false){
                alert('Please select the checkbox');
                return false;
            }
            else{
                Callingcontrollermetod();
            }
        });
    });
</script>
    <apex:actionFunction name="Callingcontrollermetod"  action="{!savemethod}" reRender=""/>
    Do u need ? &nbsp;  <input type="checkbox" id="chk2"/><br/>
    <apex:commandButton onclick="fn()" id="btn" value="Save"/>
</html>
</apex:form>
</apex:page>

Controller : 
---------------------
public with sharing class savecontroller {
    Public void savemethod(){
        system.debug('I am called');
    }
}

Thanks.