2

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.

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Works, I marked it as answer. Thank you very much for your effort.
0

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.

1 Comment

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.
0

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.

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.