In my solution, the user can add pictures & attachments which will be displayed on the web. The media content & the file name is stored in the table
I want to restrict the file name so that it does not have special characters. When the user selects a file I want to check whether it has any special characters (including spaces) and want to replace all those characters with underscore.
Have a look at the JavaScript Reference and search for “regular expression”. With a regExp you can filter out any character you don’t like to have or, other way, remove all characters that you don’t want.
Have had to deal with this problem in other environments. It can be nasty. Watch out for creating duplicate filenames when you strip out the special characters. You may need logic to add ‘-1’ or some other string if the stripping creates a dupe. If you add such a string, you have to be sure you don’t exceed OS filename length limits. Also, you have to decide whether you’re adding such a string inside or outside of the file extension (file-1.pdf or file.pdf-1). I lost a lot of sleep over this–fixing what I initially thought was a small issue! Good luck.