views:

1662

answers:

4

code:

<span>Upload Adobe Acrobat file<img src="../../Images/UI/pdf.jpg" style="height: 25;
    width: 20" height="25" width="20" /></span>
<asp:FileUpload ID="uplPdf" runat="server" />
<asp:RegularExpressionValidator ID="valPdf" runat="server" ErrorMessage="Only PDF files are allowed!"
    ValidationExpression="^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))(.pdf)$" Display="Dynamic"
    ControlToValidate="uplPdf" ValidationGroup="upload" />
<br />
<asp:Button ID="btnUpload" runat="server" Text="Upload files" OnClick="btnUpload_Click"
    ValidationGroup="upload" />

code behind:

if (valPdf.IsValid && uplPdf.HasFile)

IsValid returns false after a valid pdf file name was enterd. Note that if no file is specified, returns true.

A: 

Well... I don't think that this regexp will ever work in this case. Try this one:

ValidationExpression="^.+.pdf$"

it should work for files ending in .pdf, which is what you want.

doesn't work, even worse; before it used to work on client-time and in server it reported false for IsValid, now, even in client it shows the error.
Shimmy
+2  A: 

Try this in your RegularExpressionValidator

<asp:RegularExpressionValidator ID="valPdf" runat="server" 
  ErrorMessage="Only PDF files are allowed!" 
  ValidationExpression=".+.(P|p)(D|d)(F|f)$" 
   Display="Dynamic" ControlToValidate="uplPdf" ValidationGroup="upload" />

Then in your codebehind, call Page.Validate("upload") before proceeding:

Page.Validate("upload");
if(valPdf.IsValid && uplPdf.HasFile)
{
   //Proceed with the upload
}

NOTE: The ValidationExpression above will match any files ending *.pdf in a case-insentive way. Therefore "c:\somepath\somefile.pdF", "somefile.pDF" and "somefile.Pdf" will all pass validation.

Jose Basilio
I use an update panel.
Shimmy
Did you try the code above? What were the results?
Jose Basilio
Works, I marked it as answer.Thank you very much for your effort.
Shimmy
A: 

Well yes, normally you would have to escape the dot.

Also, in the question there's used a POSIX Regex Function, while normally we use Perl Styled Expressions in .NET

Have you tried something like:

\.pdf$

And of course ensure that it ignores the case of the letters.

Claus Jørgensen
A: 

I added the file list to a string collection in the project settings.

Then I set the regex programmatically:

public static string PrepareValidExtensionRegex(string[] allowedFileTypes)
{
    for (int i = 0; i < allowedFileTypes.Length; i++)
    {
        char[] chars = allowedFileTypes[i].ToCharArray();
        int length = chars.Length;
        string[] ret = new string[length];
        for (int j = 0; j < length; j++)
        {
            char ch = chars[j];
            char replacement = char.IsLower(ch)?
                               char.ToUpper(ch): 
                               char.ToLower(ch);
            ret[j] = "[" + ch + replacement + ']';
        }
        allowedFileTypes[i] = '(' + string.Join("", ret) + ')';
    }

    return @"^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))(" + 
        string.Join("|", allowedFileTypes) + ")$";
}

//Usage:
string[] allowedFiles = new string[] { "jpg", "gif" };
RegexValidator.ValidationExpression = PrepareValidationExpression(allowedFiles);
Shimmy