problem with regular expression validator on asp.net page

  • Thread starter Thread starter vtxr1300
  • Start date Start date
V

vtxr1300

I'm having a problem with a regular expression in conjunction with the
regular expression validator. I am trying to make sure that when a user
browses for a file to upload, it ends in gif, jpeg or jpg. I have the
following expression which validates fine in a .net tester I use and
also a javascript tester. But when I use the following path on the
page, it gives me the error message that I haven't entered a valid
image.

(\.gif|\.jp[e]?g|\.png|\.JP[E]?G|\.GIF|\.PNG)$

C:\Documents and Settings\Geoff\Desktop\mypic.jpg

What am I doing wrong here?
 
In
vtxr1300 said:
I'm having a problem with a regular expression in conjunction with the
regular expression validator. I am trying to make sure that when a
user browses for a file to upload, it ends in gif, jpeg or jpg. I
have the following expression which validates fine in a .net tester I
use and also a javascript tester. But when I use the following path
on the page, it gives me the error message that I haven't entered a
valid image.

(\.gif|\.jp[e]?g|\.png|\.JP[E]?G|\.GIF|\.PNG)$

C:\Documents and Settings\Geoff\Desktop\mypic.jpg

What am I doing wrong here?

A regular expression validator requires the regexp to match the whole input
field not just part of it. You are matching only the ending (file
extension). The following will do what you want:

..+(\.gif|\.jp[e]?g|\.png|\.JP[E]?G|\.GIF|\.PNG)$

In fact it will insist on the form filename.ext where ext is validated as
you have it and filename must be at list one character. As it is, it will
accept any character(s) for the name; you may want to change it to match
only characters that are legal in file name.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top