如何使用jquery validate验证asp的两个实例:FileUpload为一个文件?

时间:2017-09-10 02:51:50

标签: jquery asp.net validation file-upload

我有两个文件上传程序可以上传pdf文件,但我不知道如何将验证限制为至少一个文件上传器。

 $(document).ready(function () {
        $('#form1').validate({
            rules: {
                FileTitle: {
                    required: true,
                    minlength: 2,
                },
                FileDescription: {
            required: true,
            minlength: 2,
            }
            },
            messages: {

            },
            errorPlacement: function (error, element) {

                    error.insertBefore(element);          
            }
        });
    });
</script>

1 个答案:

答案 0 :(得分:1)

只需浏览下面的代码即可。

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="CS.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

    <title>Untitled Page</title>
<script type ="text/javascript">
    var validFilesTypes=["pdf"];
    function ValidateFile()
    {
        var isValidFile2 = false;

        var isValidFile = false;
        var label = document.getElementById("<%=Label1.ClientID%>"); 


        var fileValue = $("#<%=FileUpload1.ClientID%>").val(); 
        var fileValue2 = $("#<%=FileUpload2.ClientID%>").val(); 
        if(fileValue=='' && fileValue2==''){
           alert("You should upload atleast one file."); 
           return false;
        }
        console.log("file" );
        console.log("file"+fileValue2);
        if(fileValue!='') 
        { 

      var file = document.getElementById("<%=FileUpload1.ClientID%>"); 
      var path = file.value;
      var ext=path.substring(path.lastIndexOf(".")+1,path.length).toLowerCase();

      for (var i=0; i<validFilesTypes.length; i++)
      { 
        if (ext==validFilesTypes[i]) 
        {
            isValidFile=true;
            break;
        }
      }



      }
      if(fileValue2!='') 
        { 
       var file2 = document.getElementById("<%=FileUpload2.ClientID%>"); 

      var path2 = file2.value;
      var ext2=path2.substring(path2.lastIndexOf(".")+1,path2.length).toLowerCase();
      for (var i=0; i<validFilesTypes.length; i++)
      { 
        if (ext2==validFilesTypes[i]) 
        {
            isValidFile2=true;
            break;
        }
      }



      }

      if (isValidFile == false && isValidFile2 == false)
      {
          alert("Invalid File. Please upload atleast one File with" +
              " extension:\n\n" + validFilesTypes.join(", ")); 
        label.style.color="red";
        label.innerHTML = "Invalid File. Please upload atleast one File with" + 
         " extension:\n\n"+validFilesTypes.join(", ");
         return false;
      }  


      return true;
     }
</script> 
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:FileUpload ID="FileUpload1" accept="pdf/*.pdf" runat="server" />
         <asp:FileUpload ID="FileUpload2" accept="images/*" runat="server" />
        <asp:Button ID="btnUpload" runat="server" Text="Upload"  
           OnClientClick = "return ValidateFile()" OnClick="btnUpload_Click"  />
        <br />
        <asp:Label ID="Label1" runat="server" Text="" />
    </div>
    </form>
</body>
</html>