I am trying to limit the user from uploading Long named files and unaccepted file extensions using a regular expression validator in Asp.net 2.0 Webforms. I wanted the filename to be withing 30 characters[restriction due to Db Design] and extensions match png,jpeg,zip,rar,7z.I came up with this regular expression which makes sure filename is valid character and then comes . literal period then the extension.
^[\w_]{3,28}\.(pdf|jpeg|png)$
Update:
i came up with this regex that matches even last instance only that even long filenames get validated
(\\(?!.*\\)|)[\w_]{3,30}\.(pdf|jpeg|jpg|pdf)$
Update: Even this works just that fileName does not get limited to 30 characters
[\w]{3,30}\.(pdf|jpeg|jpg|pdf)(?=)
info: It even validated c:\fakePath\stackoverflowstackoverflowstackoverflowstackoverflow btw it highlighted last 30 characters in fileName.
The problem was that i want the validation to be done on client at time of uploading hence EnableClientScript = True , but to my surprise the fileUpload.value which the control validates was totally different in most of the browsers.
Firefox reports: filename.extension as expected,
Internet Explorer reported the Full Path to File
chrome reports C:\fakePath\fileName.extension
Now how do i write a expression for this, my idea was to look for last instance of the \ character if present in the value then begin form there. But my knowledge of Regex is totally limited to the basics. could someone correct me and provide a working regex,
Note:
Pls , before providing one make sure it met my requirements, works cross browser and validates on clientSide.
I suggest this:
This matches a filename starting at a word boundary (without the
\bit might start the match in the middle of an overlong filename). Only ASCII letters, digits and underscores are allowed in the filename (no accented characters!), dots are also forbidden.Remember to construct the regex using regex literal notation: