2

我需要对文件上传控件进行多次验证。我现在有以下代码:

<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" />

我需要添加这个^(?!.. )(?!. ..)(?=. [^.]$)[^\"#%& :<>?\/{|}~]{1,128} $正则表达式验证从这里除了必填字段验证器。我该怎么做?

4

1 回答 1

3

更新:

您可能可以改用正则表达式,以允许反斜杠到文件名并禁止在文件名中使用它们,但是这种野兽的复杂性可能不值得花费时间和精力来构建它。

由于原始正则表达式用于验证用户输入文件名的文本框(而不是由操作系统生成名称的文件输入),我认为更好的做法是使用<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.

原来的:

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

您可以将任意数量的验证器控件指向单个输入。

只需设置适当的属性(例如ValidationExpression中的<asp:RegularExpressionValidator>)并确保该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}$" />

您可能还想查看验证组

于 2012-11-10T03:52:18.893 回答