asp.net - Regex to check special characters in the filename in an input directory/path -
i have fileupload control , need validate filename browsed using regularexpressionvalidator. i'm using regex: ^[a-za-z0-9_@.-]*$
. detecting special characters in directory. e.g. \
or :
in c:\sample file.txt
. other catch is required use static class store regex expression. on load of user control contains fileupload control, there like, this.regexvalidatorcontrol.validationexpression = sampleclass.filenameregex
, filenameregex declared static string ^[a-za-z0-9_@.-]*$
.
any on how should approach this?
there 2 requirements:
- regularexpressionvalidator should used.
- the regex stored in static member of static class.
i think following provide decent match windows filename/directory structure:
^[a-z]:(\\[^\\/:*?"<>|]+)+.\w+$
(note blacklist came windows explorer after trying enter in bad character.)
example matches:
$ grep -p '^[a-z]:(\\[^\\/:*?"<>|]+)+.\w+$' file_list.txt c:\sample file5.txt c:\program files (x86)\windows\file.exe c:\program files\jon's folder\file.pdf c:\my-file.txt c:\5@^&[]{}!#i.txt.exe
if want change whitelist, modify accordingly:
$ grep -p '^[a-z]:(\\[\w\s@-]+)+.\w+$' file_list.txt c:\sample file5.txt c:\my-file.txt
Comments
Post a Comment