Working with list item attachments is not the best option, but it makes sense at certain points. For example it is a good place to store a pdf-version of your list item or other additional information to the listitem.
If you add an attachment to the list, SharePoint stores the listitem and then tries to upload your attachment. If the attached file has an incorrect filename or characters which are not allowed, the file will not be attached, but the list item will be saved. That’s not so cool, if you’re running workflows on your list.
One possibility is to validate the file name before the save event occurs. I found the script in this post.
This is helpfully for validating the filename of the file itself, but what if you want to check if the filename was already used by another attached file. In SharePoint you cannot add two files as attachment with the same filename. I found this post which explains it really good and also provides a solution by using the following code which i copied here (thanks to the author!)
Now the user gets a message that the attached file with its filename already exists and that he should use another filename for attaching. The item will not be saved so if there is an workflow which needs the attachments it won’t fire before the file is attached correctly.The article or information provided here represents completely my own personal view & thought. It is recommended to test the content or scripts of the site in the lab, before making use in the production environment & use it completely at your own risk. The articles, scripts, suggestions or tricks published on the site are provided AS-IS with no warranties or guarantees and confers no rights.