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

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Pages / file attachment valida...
Power Pages
Suggested Answer

file attachment validation using basic forms

(1) ShareShare
ReportReport
Posted on by 142
Hi,
 
I am using the basic form since it had the built in captcha function. i wanted to add custom file validation logic to my attachments. Ex, column and row check. I have added the JS code but when i trace the issue. Seems like it cannot find the actual file to validate.
 
If i am doing a normal html input alone and have a JS code to validate. Seems like its working well.
 
Anyone has any experience on how to make the custom file validation work in basic forms?
Categories:
I have the same question (0)
  • Suggested answer
    Fubar Profile Picture
    8,352 Super User 2025 Season 2 on at
    You may need to provide more information about what validations you are wanting to implement and also what mechanism you are using for the Files (as there are different ways to attach files. e.g. Configured as a Notes attachment or Azure Blob, or are you using a Column of type File?)
     
    e.g. If a Notes attachment the file content gets stored in the documentbody field of the Note (annotation) as a base64 string. If a Blob the Notes attachment contains JSON that contains the location of the file in the Blob storage.
     
     
     
  • surya narayanan Profile Picture
    102 on at
    Hi,
    • The file upload control in Basic Forms (especially when using Attachment or File columns) is rendered by Power Pages using iframes or Shadow DOM, or is deeply nested in dynamically generated markup.

    • Standard document.querySelector('input[type=file]') may not find it.

    • You may also run into timing issues: the control may not be rendered when your script runs.

    Step-by-step approach:

    1. Add your custom file input (optional but easier)

    Instead of using the out-of-the-box Attachment field, use a custom HTML file input in your Basic Form Web Page:

    <input type="file" id="customFileUpload" accept=".csv,.xlsx" />
    <div id="fileErrorMsg" style="color:red;"></div>
     

    Place this in the Custom HTML (Web File or inside the form).

    2. Add JS for validation

    This example checks for Excel files with a column "Email" and at least 2 rows:

    <script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.18.5/xlsx.full.min.js"></script>
    <script>
    document.addEventListener('DOMContentLoaded', function () {
    const fileInput = document.getElementById("customFileUpload");
    const errorMsg = document.getElementById("fileErrorMsg");
    let fileIsValid = false;
    
    fileInput.addEventListener("change", function (e) {
    const file = e.target.files[0];
    if (!file) return;
    
    const reader = new FileReader();
    reader.onload = function (evt) {
    const data = evt.target.result;
    const workbook = XLSX.read(data, { type: "binary" });
    const sheetName = workbook.SheetNames[0];
    const sheet = workbook.Sheets[sheetName];
    const json = XLSX.utils.sheet_to_json(sheet, { header: 1 });
    
    // Check header and row count
    const headerRow = json[0];
    if (!headerRow.includes("Email")) {
    errorMsg.textContent = "Missing required column: Email";
    fileIsValid = false;
    return;
    }
    if (json.length < 2) {
    errorMsg.textContent = "File must contain at least 1 data row.";
    fileIsValid = false;
    return;
    }
    
    errorMsg.textContent = "";
    fileIsValid = true;
    };
    reader.readAsBinaryString(file);
    });
    
    // Hook into form submit
    document.querySelector("form").addEventListener("submit", function (e) {
    if (!fileIsValid) {
    e.preventDefault();
    errorMsg.textContent = "Please upload a valid file before submitting.";
    }
    });
    });
    </script>

    3. Hiding the default attachment control

    You can hide the default file upload control rendered by Power Pages:

    div[data-name='your_field_schema_name'] {
    display: none !important;
    }

    This allows you to use your own custom control and logic instead.

    4. (Optional) Storing the file in Dataverse

    If you're bypassing the built-in file upload field, and still want to store the uploaded file in Dataverse, you'll need to:

    • Use a custom Web API call in JS Or attach it in a plugin/Power Automate after form submission

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

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Pages

#1
Fubar Profile Picture

Fubar 74 Super User 2025 Season 2

#2
Jerry-IN Profile Picture

Jerry-IN 55

#3
sannavajjala87 Profile Picture

sannavajjala87 31

Last 30 days Overall leaderboard