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:

  1. regularexpressionvalidator should used.
  2. 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

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -