Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Power Pages - Customize & Extend
Unanswered

Set File Field Required with JavaScript

(0) ShareShare
ReportReport
Posted on by

Hi All!!

Within Power Pages I am trying to set the attach a file as required on a basic form when a Change Request = Bank Account Information with JavaScript.

 

I have tried the following with JavaScript trying to set adx_attachfilerequiredfield as true but cannot get it to work. Any ideas for the script would be appreciated!

Categories:
  • Fubar Profile Picture
    7,964 Super User 2025 Season 1 on at
    Re: Set File Field Required with JavaScript

    @GWham1 with validators it is probably better to add them first, and then put the logic inside the validator e.g. field1 = "x" then .... if you add the validator inside an onchange then you also need to either remove the validator or check for its existence to avoid the same validator getting added multiple times (e.g. when someone toggles the field the onchange is on) - when this happens the each validator object gets called and the user sees multiple of the same error messages.

  • GWham1 Profile Picture
    on at
    Re: Set File Field Required with JavaScript

    Something like the following might work. You will need to call the addValidator function with your file field ID when the change request is selected.

     

     

    $(document).ready(function() {
     // trigger on change?
     $("#changerequest").click(function() {
     if ($(this).val('Bank Account')) {
     // set mandatory
     addValidator ("gw_file", "Select a file");
     }
     });
    
     // add validator
     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); });
     }
    
    });

     

     

  • oliver.rodrigues Profile Picture
    9,319 Most Valuable Professional on at
    Re: Set File Field Required with JavaScript

    Hi, you should add an actual form validation and not just set the class: Add custom JavaScript to a form | Microsoft Learn

     

  • CU20091748-0 Profile Picture
    36 on at
    Re: Set File Field Required with JavaScript

    I think you would need to bind the function to the change event of the choice field. You would have this in your main function:

    function updateRequiredClass(); {
    
    //retrieve value of fields
    
    
    
    //define condition under which the dependent field should be required
    
    
    }
    
    
    $("#choice_field").change(function() {
    
    
    
    updateRequiredClass();
    
    
    
    });

     

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

Michael Gernaey – Community Spotlight

We are honored to recognize Michael Gernaey as our June 2025 Community…

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard > Power Pages

#1
Fubar Profile Picture

Fubar 69 Super User 2025 Season 1

#2
oliver.rodrigues Profile Picture

oliver.rodrigues 49 Most Valuable Professional

#3
Jon Unzueta Profile Picture

Jon Unzueta 43

Featured topics