Join the Team Forms community

141
members
11
new
J
d
B
R
M

Prevent attachement titles from containing special characters.

Last updated 3 weeks ago

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?

E
Erin Dwyer
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
E
Erin Dwyer

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.