White list regular expression validator

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

White list regular expression validator

Giovanni Acerbi
To allow only certain files to be uploaded, using the multifile control, I was using the regular expression I found in the demo.aspx file that is

ValidationExpression="([^.]+[.](jpg|gif|png|JPG|GIF|PNG); *)*$"

It seems to me this expression will fail if there is another . (dot) in the file path.

For example the following one will fail

c:\documents and settings\pc.user\documents\images\mypic.jpg

I tried to modify the original expression into the following one and it seems to be working great, but I am not a regex expert, so please let me know if you think it could fail in any way

ValidationExpression="^(.*[\.](jpg|gif|png|JPG|GIF|PNG); *)*$"

Best regards.
Giovanni Acerbi.
Reply | Threaded
Open this post in threaded view
|

Re: White list regular expression validator

Dean Brettle
Administrator
I believe that your regexp will work even if only the last filename in the list has one of the allowed extensions.  I think it would accept "foo.exe; bar.jpg;" because "foo.exe; bar" would all match the ".*" part of the regexp.  I think what you are looking for is:

^([^;]+[.](jpg|gif|png|JPG|GIF|PNG); *)*$

--Dean
Reply | Threaded
Open this post in threaded view
|

Re: White list regular expression validator

Giovanni Acerbi
You are right Dean!

Your regex is working great.

Thank you so much.

Best regards.

Giovanni Acerbi.