Regularexpressionvalidator verifies fileupload)

Source: Internet
Author: User
ASP. NET 2.0 life savior-regularexpressionvalidator for fileupload Control

Why did I say it was a life savior? I believe this tip is very handy for everyone. This was a problem faced by collegou. So I took up the challenge to look into this.

If you browse the Internet you will find a lot of people recommending using regularexpression validator to validate the fileupload control on certain file formats to be used. some disagree on this and recommend on servervalidate. take note, I am not here to debate on which one to use. I am here to blog on just regularexpressionvalidator on fileupload control and explains the best way to achieve the right result. let me know what you think of thisJ

Below is the sample code:

<%@ Page Language= "C #" Autoeventwireup= "True" Codefile= "Default. aspx. cs" Inherits= "_ Default" %>

 

<!Doctype Html Public "-// W3C // dtd xhtml 1.0 transitional // en" Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>

<Html Xmlns= "Http://www.w3.org/1999/xhtml">

<Head Runat= "Server">

<Title>Untitled page</Title>

</Head>

<Body>

<Form ID= "Form1" Runat= "Server">

<Div>

Asp : button id =" btndeleteimg " runat =" server " text =" delete image " onclick =" btndeleteimg_click " /> Br />

Asp : fileupload id =" fupload " runat =" server " width =" 550px " /> & nbsp;

<ASP:Button ID= "Btnupload" Runat= "Server" Text= "Upload" Width= "76px" Onclick= "Btnupload_click" />& Nbsp;

< ASP : Regularexpressionvalidator ID= "Refimage" Setfocusonerror= "True" Runat= "Server" Controltovalidate= "Fupload"

Errormessage= "Upload ipvs and GIFs only" Validationexpression= "^ ([A-Za-Z] :) | (" "{2}" W +) "$ ?) ("" ("W [" W]. * ))(.jpg |. jpg |. gif |. GIF) $"

Display= "Dynamic"> </ASP:Regularexpressionvalidator>

</Div>

</Form>

</Body>

</Html>

 

So the expression is like this"^ ([A-Za-Z] :) | (" "{2}" W +) "$ ?) ("" ("W [" W]. * ))(.jpg |. jpg |. gif |. GIF) $"Which is used in this scenario.

Yes It works entirely fine on the validation. But soon later you will realize a problem.

Let's visualize this in a table view based on the code above.

[delete button

[File Upload control]

[upload control]

[Regularexpressionvalidator]

 

When you try to browse a music file, you will see an error like below:

[Delete button

[File Upload control] C:/sgdotnet/elephant.pdf

[upload control]

[Regularexpressionvalidator]Upload ipvs and GIFs only

 

When you try to browse a proper image this time, the error will disappear

[Delete button

[File Upload control] C:/sgdotnet/tortoise.jpg

[upload control]

[Regularexpressionvalidator]

 

Now guess what when you press upload which will cause a PostBack, not only the image is being saved but the error message will suddenly appear. take note, you definitely do not want to see this right.

[delete button

[File Upload control]

[upload control]

[Regularexpressionvalidator]Upload ipvs and GIFs only

** When you upload the image file, the file upload's textbox will be cleaned

So you will be shocked? What is the problem here? It doesn' t make sense for the error message to appear.

Then I came into a conclusion that there might be a slight possibility on the regularexpression validateexpression. take note, I am not so good in regularexpression but I believe it helps me to achieve results here and definitely in this context.

So I came out with this instead:

"^. +". (JPG) | (GIF) | (JPEG) | (PNG) | (BMP) $"

It solved the problem. yes. thanks God and I was very happy that I was able to solve this problem. most people on the Internet recommended the 1st way and honestly speaking it did validated, but the error will occurred on PostBack.

Have fun and hope you find this useful for you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.