web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Pages / make attachment mandat...
Power Pages
Suggested Answer

make attachment mandatory if checkbox is selected

(0) ShareShare
ReportReport
Posted on by 337
I am trying to make the attachment field mandatory only if checkbox is selected.
I tried below JScript but not working.
 
/*for attachment field*/
document.getElementById("cr4f0_certificatecheckbox").addEventListener('change', function(){
  if(this.checked)
  {
  document.getElementById("AttachFileLabel").required = false;
  }
  else
  {
  document.getElementById("AttachFileLabel").required = true;
  }
});
 
 
Categories:
I have the same question (0)
  • Suggested answer
    Fubar Profile Picture
    8,487 Super User 2026 Season 1 on at
    For the mandatory (red asterisk) you will probably have to add the class to the parent not the on the label tag directly
    $("#NewAttachFileLabel").parent().addClass("required");
    Note: this will only put the asterisk not force it to be mandatory, to check it has been populated you will need to check if the field is populated in a custom validator https://learn.microsoft.com/en-us/power-pages/configure/add-custom-javascript#additional-client-side-field-validation
     
  • Suggested answer
    GWham Profile Picture
    58 Moderator on at
        //eg. addValidator("customerid", "Customer")
        function addValidator(fieldName, fieldLabel) {
    
    
            if (typeof (Page_Validators) == 'undefined') return;
            // Create new validator
            $("#" + fieldName + "_label").parent().addClass("required");
    
            var newValidator = document.createElement('span');
            newValidator.style.display = "none";
            newValidator.id = "RequiredFieldValidator" + fieldName;
            newValidator.controltovalidate = "";
            newValidator.errormessage = "<a href='#" + fieldName + "_label'>" + fieldLabel + " is a mandatory field.</a>";
            newValidator.validationGroup = "";
            newValidator.initialvalue = "";
            newValidator.evaluationfunction = function () {
                var value = $("#" + fieldName).val();
                if (value == null || value == "") {
                    return false;
                } else {
                    return true;
                }
            };
    
            // Add the new validator to the page validators array:
            Page_Validators.push(newValidator);
    
            // Wire-up the click event handler of the validation summary link
            $("a[href='#" + fieldName + "_label']").on("click", function () { scrollToAndFocus(fieldName + '_label', fieldName); });
        }
    Try to add a custom validator for the upload field when the checkbox is selected.

    You will also need to remove it when deselected.
     
    //eg. removeValidator("customerid")
    function removeValidator(fieldName) {
      var count = 0;
      for(var i =0; i < Page_Validators.length - count; i++){
        if (Page_Validators[i - count].id == "RequiredFieldValidator" + fieldName) {
          Page_Validators.splice(i - count, 1);
          count++;
        }
      }
      $("#" + fieldName + "_label").parent().removeClass("required");
    }
     
     
     
     
     
  • Suggested answer
    Inogic Profile Picture
    1,277 Moderator on at
    Hi,
    To prevent a user from submitting a form based on specific conditions in Power Pages, relying solely on the "required" field may not be effective, especially if the field is not marked as required in CRM.
    A possible approach is to implement custom logic to control the form submission. For example, if a checkbox is selected but the user has not attached a document, you can create a secondary button to validate this condition.
    Here’s how it works:
    • The secondary button checks whether the document is attached when the checkbox is checked.
    • If the document is not attached, the button triggers an alert notifying the user with a message like "Please attach the document."
    • The actual submit button is hidden and will only be triggered by the secondary button if the condition is met.
    This ensures the form cannot be submitted until the required conditions are satisfied.
     
    Hope this helps.
     
    Thanks!
    Inogic Professional Services: Power Platform/Dynamics 365 CRM
    An expert technical extension for your techno-functional business needs
    Drop an email at crm@inogic.com 
    Service: https://www.inogic.com/services/ 
    Tips and Tricks: https://www.inogic.com/blog/ 

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Users!

Kudos to our 2025 Community Spotlight Honorees

Congratulations to our 2025 community superstars!

Congratulations to the March Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Pages

#1
rezarizvii Profile Picture

rezarizvii 64

#2
DP_Prabh Profile Picture

DP_Prabh 36

#3
oliver.rodrigues Profile Picture

oliver.rodrigues 30 Most Valuable Professional

Last 30 days Overall leaderboard