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

Community site session details

Session Id :
Power Pages - General Discussions
Suggested answer

restrict file input with javascript

(0) ShareShare
ReportReport
Posted on by 31
I'm trying to restrict a file input on my power pages to just word documents. The JavaScript I use gives an alert when it is not a word document but still add the file.  The preventdefault has no effect on the file upload. is their a way to prevent the file from being attach anyway.
 
var LimitFileUpload = function(fieldName, fileEvent){
    try{
        const allowedExtensions = ['doc', 'docx'];
 
const fileInput = document.getElementById(fieldName);
 
   const file = fileEvent.target.files[0];
  const fileExtension = file.name.split('.').pop().toLowerCase();
 
  if (!allowedExtensions.includes(fileExtension)) {
    alert('Invalid file type. Only word documents are allowed.');
    fileEvent.target.value = ''; // Clear the file input
    fileEvent.preventDefault();
    return false;
  }
    }
    catch(error)
    {
        errorHandler(error);
    }
}
 
 
Categories:
I have the same question (0)
  • Suggested answer
    Fubar Profile Picture
    8,224 Super User 2025 Season 2 on at
    restrict file input with javascript
    What type of file upload are you using? (Notes attachment, or File field)
     
    If using Notes you should be able to set the Mime types that are accepted - if not exposed in the Designer use the Power Pages Management app (I have had issues in the Designer where the mime type option doesn't show sometimes). Also if using notes there can be multiple places depending on how it is being implemented e.g. basic form has a general Notes section, but if using a Metadata record for Notes has areas under the Create & Edit dialogs.
  • knparksnyc Profile Picture
    31 on at
    restrict file input with javascript
     
    Thanks for reply. I have an input file with chose file button that opens a dialog to choose a file, not using notes:
     

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Chiara Carbone – Community Spotlight

We are honored to recognize Chiara Carbone as our Community Spotlight for November…

Leaderboard > Power Pages

#1
Fubar Profile Picture

Fubar 50 Super User 2025 Season 2

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 38 Super User 2025 Season 2

#3
Jerry-IN Profile Picture

Jerry-IN 30

Last 30 days Overall leaderboard