Join the Team Forms community

Updated 5 months ago

Prevent attachement titles from containing special characters.

At a glance
The community member asked for JavaScript code to prevent users from uploading attachments with special characters like "'", "&", or "%". Another community member provided a solution using custom validation in a file component, with a function to detect special characters and set a custom error message. The original community member acknowledged that Team Forms handles special characters well, but they are using the SharePoint Get File action in Power Automate which does not like apostrophes in file names, so they need this custom validation solution.

What would the JavaScript code look like to prevent users from uploading an attachment that contains a special character like "'" or "&" or "%"? Where would it be added in the file object?

Marked as solution

Hi Thomas,

You could use a custom validation to show an error any time a user uploads a file with special characters. To do this you would go to the validation tab of your file component and navigate to "Custom Validation". Here you can add some JavaScript code to make the response invalid whenever a special character is detected. You can adapt the example below to fit your specific requirements:

JavaScript
function containsSpecialChar(fileName){
    return fileName.includes('%') || fileName.includes('$')
}

valid = !instance.getValue().some(file => containsSpecialChar(file.name))


Under the validation tab there is also a "Custom Error Message" property that you may want to set to display a custom error message when the validation fails. For example:

Your file name contains special characters, please re-name the file and try again.


Please note that Team Forms automatically handles special characters in file names. It's worth looking at why you need to do this to begin with as this is will likely be a poor experience for your users and add complexity to your form.

View full solution
E
T
1 comment·1 reply

Hi Thomas,

You could use a custom validation to show an error any time a user uploads a file with special characters. To do this you would go to the validation tab of your file component and navigate to "Custom Validation". Here you can add some JavaScript code to make the response invalid whenever a special character is detected. You can adapt the example below to fit your specific requirements:

JavaScript
function containsSpecialChar(fileName){
    return fileName.includes('%') || fileName.includes('$')
}

valid = !instance.getValue().some(file => containsSpecialChar(file.name))


Under the validation tab there is also a "Custom Error Message" property that you may want to set to display a custom error message when the validation fails. For example:

Your file name contains special characters, please re-name the file and try again.


Please note that Team Forms automatically handles special characters in file names. It's worth looking at why you need to do this to begin with as this is will likely be a poor experience for your users and add complexity to your form.

Erin,

Thanks for the response and solution.

Team Forms does handle special characters perfectly when using all the Team Forms actions in Power Automate. However, because I need to gather a lot of different files from SharePoint including Team Forms, I am forsed to use the SharePoint Get File action in Power Automate which does not "like" apostrophes (') in file names.


Thanks again for the solution.