我正在尝试验证 FileUpload
控件以检查用户只能选择 10 个图像。我已经有了服务器端代码。但我想在客户端验证它。
代码:
<asp:FileUpload ID="FileUpload2" CssClass="myButton1" multiple="multiple"
runat="server" />
<asp:Button ID="Btnuploadimages" runat="server" CssClass="myButton"
Style="margin-top: 2px;" OnClientClick="return ValidateFile2()"
Text="Upload" OnClick="Btnuploadimages_Click" />
最佳答案
在您的 ValidateFile2()
方法中,您需要添加以下代码,以便可以从文件上传控件
获取所选文件计数
>。您可以通过检查以下条件来验证所选文件的数量。
客户端代码: JavaScript
function ValidateFile2(){
var fileCount = document.getElementById('filesToUpload').files.length;
if(fileCount > 10) // Selected images with in 10 count
{
alert("Please select only 10 images..!!!");
return false;
}
else if(fileCount <= 0) // Selected atleast 1 image check
{
alert("Please select atleat 1 image..!!!");
return false;
}
return true; // Good to go
}
JQuery 和 Javascript 代码可在我的工作中找到 JS Fiddle
关于javascript - 设置多个文件上传的验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26814577/