【发布时间】:2020-02-04 04:46:21
【问题描述】:
如何在多文件上传控件中选择最少三个文件
我应该添加 AJAX 文件上传控制工具包
我想为文件上传控制选择最少三个文件,当我选择少于三个文件时,我想在客户端本身发出警报消息。我应该在哪里更改现有代码? 我哪里做错了请指导我:
我在 aspx 中的代码:
<label>Root Cause Image<span style="color: #ff0000;">*</span></label>
<asp:FileUpload ID="FileUpload_RootCause" runat="server"
accept=".png,.jpg,.jpeg,.gif"
multiple="multiple"
type="file"
name="image[]" />
<asp:RegularExpressionValidator ID="rev1" runat="server"
ControlToValidate="FileUpload_RootCause"
required="This Field is Required"
ErrorMessage="Only JPG and PNG are allowed"
ValidationExpression=".*((\.jpg)|(\.JPG)|(\.png)|(\.PNG))"
CssClass="red">
</asp:RegularExpressionValidator>
<script>
$("#FileUpload_RootCause").on("change", function ()
{
if ($("#FileUpload_RootCause")[0].files.length < 3)
{
alert("You have to select minimum 3 images to proceed Further");
}
else
{
$("#imageUploadForm").submit();
}
});
</script>
上面的代码根本不工作不知道为什么?,我尝试了第二种方法,我尝试了类似的方法
我得到了和以前一样的东西,它没有发出任何警报消息,为什么?以及我做错了什么
<asp:FileUpload ID="FileUpload_RootCause" runat="server"
accept=".png,.jpg,.jpeg,.gif" AllowMultiple="true" />
<script>
function ValidateFile2() {
var fileCount =
document.getElementByID('FileUpload_RootCause').files.length;
if (fileCount < 3)
{
alert("Please select minimum 3 images..!!!");
return false;
}
else if (fileCount <= 0)
{
alert("Please select at-list 1 image..!!!");
return false;
}
return true;
}
</script>
【问题讨论】:
-
您在 RegularExpressionValidator 上有一个“必需”属性。我不认为这是有效的。与 asp:FileUpload 控件上的所有属性相同。也许尝试上传 html?
-
但我只是尝试了你所拥有的,它在 Firefox 和 Chrome 中对我有用。
-
我也在使用 Chrome,但在我的情况下它不起作用??
-
这里没有关于 jQuery Validate 插件的内容。编辑标签。
-
@sparky 我应该添加什么??
标签: javascript c# jquery asp.net validation