2012-11-10 117 views
2

我有一个要求对文件上传控件执行多个验证。我有以下代码现在:ASP.NET文件上传验证

<asp:Button id="btnUploadFile" class="ms-ButtonHeightWidth" runat="server" OnClick="UploadFile_Click" Text="Upload File"></asp:Button> 
<asp:RequiredFieldValidator ID="InputFileValidator" ControlToValidate="InputFile" Text="You must specify a value for the required field" runat="server" /> 

我需要添加此^(= [^] $?)[^(..?!)(..?!)。 \“#%& :<> \/{|}〜]除了所需的字段校验从here {1128} $正则表达式验证我如何做到这一点

回答

3

UPDATE:

你可能可以ada正则表达式反而允许反斜杠到文件名,并且不允许它们在文件名中,但是这种野兽的复杂性可能不值得花时间和精力去构建它。

由于原始正则表达式用于验证用户输入文件名(而不是文件输入,其中名称由操作系统生成)的文本框,因此我认为更好的方法是使用<asp:CustomValidator>控件取而代之的是分割\的值以获得更容易解析的块。

这种方法的主要优点是可以将复杂的正则表达式分解为多个更简单(并且更容易理解)的正则表达式,并一次一个针对文件名测试它们。

<script type="text/javascript"> 
     var validateFile = function validateFile(sender, args) { 
      'use strict'; 
      var fileWithPath, //split on backslash 
       fileName = fileWithPath[fileWithPath.length - 1], //grab the last element 
       containsInvalidChars = /["#%&*:<>?\/{|}~]/g, //no reason to include \ as we split on that. 
       containsSequentialDots = /[.][.]+/g, //literal .. or ... or .... (etc.) 
       endsWithDot = /[.]$/g, // . at end of string 
       startsWithDot = /^[.]/g, // . at start of string 
       notValid = false, //boolean for flagging not valid 
       valid = fileName.length > 0 && fileName.length <= 128; 
      notValid = containsInvalidChars.test(fileName); 
      notValid = notValid || containsSequentialDots.test(fileName); 
      notValid = notValid || endsWithDot.test(fileName); 
      notValid = notValid || startsWithDot.test(fileName); 
      args.IsValid = valid && !notValid; 
     }; 
    </script> 
    <asp:FileUpload ID="InputFile" runat="server" /> 
    <asp:RequiredFieldValidator ID="rqfvInputFile" runat="server" ControlToValidate="InputFile" ErrorMessage="File is required"></asp:RequiredFieldValidator> 
    <asp:CustomValidator ID="cstvInputFile" runat="server" ControlToValidate="InputFile" ClientValidationFunction="validateFile" ErrorMessage="File is not a sharepoint file"></asp:CustomValidator> 
    <asp:Button ID="Button1" runat="server" Text="Button" /> 

一个警告上述是文件名组块被分割上\,这是不太可能是用于Unix或Mac系统路径分隔符。如果您需要这对这些客户端运行为好,那么就要拆就要么\/,你应该能与此做:

var filePath = args.Value.split(/\\|\//g); //not tested. 

ORIGINAL:

添加在<asp:RegularExpressionValidator>控件中,并将ControlToValidate属性设置为您的文件上传器控件。

您可以拥有尽可能多的验证器控件,只要您指向一个输入即可。

只需设置适当的属性(例如<asp:RegularExpressionValidator>中的ValidationExpression),并确保ControlToValidate属性指向要验证的输入。

例子:

<asp:Button id="btnUploadFile" class="ms-ButtonHeightWidth" runat="server" OnClick="UploadFile_Click" Text="Upload File"></asp:Button> 
<asp:RequiredFieldValidator runat="server" ID="RequiredInputFileValidator" ControlToValidate="InputFile" Text="You must specify a value for the required field" /> 
<asp:RegularExpressionValidator runat="server" ID="RegexInputFileValidator" ControlToValidate="InputFile" ErrorMessage="Only valid SharePoint files are allowed." 
    ValidationExpression="^(?!..)(?!...)(?=.[^.]$)[^\"#%&:<>?\/{|}~]{1,128}$" /> 

您可能还需要寻找到Validation groups

+0

我想这一点,但只要我浏览并添加一个文件,正则表达式验证失败。这可能是由于正则表达式中的':'。我如何进行验证,以便仅检查文件名而不检查路径? – JSRookie

+0

@JSRookie:已更新的答案。 – pete

+0

完美的作品!我会记住Unix/mac文件路径名。谢谢。 – JSRookie