Вопрос

I am using fileupload control to upload file and at same time using regular expression for validating file name.

I want following file extension to be uploaded for .doc, .docx, .pdf i use following command to valid file name

ValidationExpression="[a-zA-Z\\].*(.doc|.DOC|.docx|.DOCX|.pdf|.PDF)$"

 <asp:FileUpload ID="FileUpload1" runat="server" CssClass="fileUpload" />
<asp:RequiredFieldValidator ID="ValidateF1" runat="server"  ErrorMessage="*" CssClass="row-validate"  ControlToValidate="FileUpload1"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="ValidateEx" runat="server"   ValidationExpression="[a-zA-Z\\].*(.doc|.DOC|.docx|.DOCX|.pdf|.PDF)$" ControlToValidate="FileUpload1" ValidationGroup="Careers" ErrorMessage="*"></asp:RegularExpressionValidator>

It fails to validate following file name

(K)+J01461+abced+high+En+(HR)(1).pdf I am not sure why it fails while it works for ABC_COMPANY_Privacy_v4.0_123456(5).pdf

Am i using the wrong validation expression. I want to allow any file name with extension as mentioned above.

Это было полезно?

Решение

try this

ValidationExpression="^.*\.(doc|DOC|docx|DOCX|pdf|PDF)$"

Другие советы

It depends on entirely on what you consider a valid file name. Your current expression only captures file names with alphanumeric characters and the character \\.

From your two examples, it looks like you want to include ()+_, as well as multiple dots, so you can fix your expression by just adding those into the character class.

ValidationExpression="[_.a-zA-Z()+\\]*\.(doc|DOC|docx|DOCX|pdf|PDF)$"

If you want to include the space character as well, then just stick that in there too.

ValidationExpression="[_.a-zA-Z()+\\ ]*\.*(doc|DOC|docx|DOCX|pdf|PDF)$"

If you want to permit all possible characters before the extension, then using Vignesh's solution will certainly do it for you.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top