ASP.net - RegEx Validator控件在FireFox中不起作用

[英]ASP.net - RegEx Validator control not working in FireFox


Works in IE and Chrome. Can't find any help on Google. Basically, it's just checking the extension of the file selected in a FileUpload control.

適用於IE和Chrome。在Google上找不到任何幫助。基本上,它只是檢查FileUpload控件中選擇的文件的擴展名。

Here's the code:

這是代碼:

        <asp:FileUpload ID="FileUpload1" runat="server" Width="450" />
       <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Invalid file type."
ValidationExpression="^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.pdf|.txt|.doc|.csv|.xls|.xlsx)$" 
ControlToValidate="FileUpload1" Display="Dynamic">
</asp:RegularExpressionValidator>

2 个解决方案

#1


4  

There are other characters in the file path in file up loader therefore, try this Regular expression instead

因此,文件加載器中的文件路徑中還有其他字符,請嘗試使用此正則表達式

(.*\.(pdf|txt|doc|csv|xls|xlsx)$)

And your RegularExpressionValidator looks like

你的RegularExpressionValidator看起來像

<asp:RegularExpressionValidator id="revImage" runat="server"
   ErrorMessage="Please Upload the Valid document File" Text="*" Display="Dynamic"
   ValidationExpression=
      "(.*\.(pdf|txt|doc|csv|xls|xlsx)$)"
   ControlToValidate="fileUploaderId"> </asp:RegularExpressionValidator>

#2


1  

The regular expression is too restrictive. I'm not sure why it works in IE & Chrome but not in Firefox. Perhaps Firefox is stripping the path of the filename?

正則表達式限制性太強。我不確定為什么它適用於IE和Chrome,但不適用於Firefox。也許Firefox正在剝離文件名的路徑?

Your reg ex is trying to match a full Windows filepath, so would fail if it the name didn't look like that, say if you used a Mac, or Linux machine or the browser pre-stripped the path from the name.

您的注冊表試圖匹配完整的Windows文件路徑,因此如果名稱看起來不那樣,則會失敗,例如,如果您使用的是Mac,或Linux機器或瀏覽器預先剝離了名稱中的路徑。

if you change it to this:

如果你改成它:

`.(pdf|txt|doc|csv|xls|xlsx)$' it only ensures the names ends with an approved extension (this just a slight improvement on what @waqas posted)

`。(pdf | txt | doc | csv | xls | xlsx)$'它只能確保名稱以批准的擴展名結尾(這只是對@waqas發布的內容略有改進)

Simon


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2011/07/11/331286ef6caa60cc127f4fd4d2ea25fc.html



 
  © 2014-2022 ITdaan.com 联系我们: