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
Jyotirupa DasJyotirupa Das 

Remove the validation message of required field on check of the checkbox field

I want to remove the salesforce validation rule on check of checkbox in VF page. It is workig for first time but if we try to submit the page without filling any of the required field, the field validation is showing up again.

User-added image

User-added image

This is the script
<script type="text/javascript">
      $(document).ready(function(){
            
        $("[id$=enabledisablecheckbox]").change(function(){
            if(this.checked==true){
                   $("[id$=FirstName]").prop("disabled",true);
                   $("[id$=LastName]").prop("disabled",true);
                    $("[id$=LastName]").prop("value",null);
                $("[id$=FirstName]").prop("value",null);
                
                if($(".anonymousFN")[0].parentElement != null){
                    $(".anonymousFN")[0].parentElement.children[0].className = '';
                    $(".anonymousLN")[0].parentElement.children[0].className = '';
                    //$(".anonymousFN")[0].value = 0;
                }
              //$("[id$=FirstName]").prop("required",false);
                 //  $("[id$=LastName]").prop("required",false);
               // $("[id$=FirstName]").required = false;
            }
            else{
                $("[id$=FirstName]").prop("disabled",false);
                $("[id$=LastName]").prop("disabled",false);
                // $("[id$=FirstName]").prop("required",true);
                  // $("[id$=LastName]").prop("required",true);
                if($(".anonymousFN")[0].parentElement != null){
                    $(".anonymousFN")[0].parentElement.children[0].className = 'requiredBlock';
                    $(".anonymousLN")[0].parentElement.children[0].className = 'requiredBlock';
                           
                }
            }
        });
    });
    </script>

here is the defination
<apex:pageblockSectionItem  >
                     <apex:outputLabel value="{!$Label.GMI_Anonymous_Contact}" ></apex:outputLabel>
                     <apex:inputCheckbox value="{!cas.AnonymousContact__c}" id="enabledisablecheckbox"  onClick="disableEnableField()" /> 
                   </apex:pageblockSectionItem >

Can someone please help!!
Vishwajeet kumarVishwajeet kumar
Hello,
You can try "Rendered" attribute on fields. Do not render those fields in page when checkbox is checked true(Instead of using "Disabled" attribute).

Thanks
Jyotirupa DasJyotirupa Das
Hi Vishwajeet,

Could you please provide me example how to do it.

Thanks
SwethaSwetha (Salesforce Developers) 
HI Jyotirupa,

Your ask seems similar to this
https://salesforce.stackexchange.com/questions/122056/how-to-remove-required-field-validation-in-a-visualforce-page

https://salesforce.stackexchange.com/questions/121496/required-field-off-when-unchecked?rq=1

Try this approach :
To make inputfields required by checkbox value, provide attribute required="{!checkboxField}" for you fields. Add AJAX support within actionSupport to your checkbox, where you could rerender required fields.
Given page utilizes opportunity standardcontroller, where you already have checkbox field:
<apex:page standardController="Opportunity">
<apex:pageMessages />
<apex:form >
    <apex:pageblock >
        <apex:pageBlockSection columns="1" id="Section"> 
            <apex:pageBlockSectionItem>
                <apex:outputLabel for="bool" value="CheckBox" />
                <apex:actionRegion>
                    <apex:inputField value="{!Opportunity.isprivate}" id="bool">
                        <apex:actionSupport event="onchange" rerender="Section"/>
                    </apex:inputField> 
                </apex:actionRegion>
            </apex:pageBlockSectionItem>
            <apex:inputField required="{!Opportunity.isprivate}" 
                value="{!Opportunity.amount}"/>
        </apex:pageBlockSection> 
        <apex:pageBlockButtons location="bottom">
            <apex:commandButton action="{!save}" value="Save"/>
        </apex:pageBlockButtons>
    </apex:pageblock>
</apex:form>
</apex:page>

actionRegion will send only checkbox field to server during validation. Save will send whole form.

If this information helps, please mark the answer as best. Thank you