Skip to main content

Notifications

Power Pages - General Discussions
Unanswered

make attachment mandatory if checkbox is selected

Posted on by 333
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:
  • Suggested answer
    Inogic Profile Picture
    Inogic 753 on at
    make attachment mandatory if checkbox is selected
    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/ 
  • Suggested answer
    GWham Profile Picture
    GWham 39 on at
    make attachment mandatory if checkbox is selected
        //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
    Fubar Profile Picture
    Fubar 7,351 on at
    make attachment mandatory if checkbox is selected
    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
     

Helpful resources

Quick Links

Welcome to the Power Platform…

We are thrilled to unveil the newly-launched Power Platform Communities!…

Community Update 9/9/24…

Power Platform Community Update…

Welcome to the new Power Platform Community!…

We are excited to announce our new Copilot Cookbook Gallery in the Community…

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 140,690

#2
RandyHayes Profile Picture

RandyHayes 76,308

#3
Pstork1 Profile Picture

Pstork1 63,355

Leaderboard

Featured topics