简体   繁体   English

用于文件上传的Java脚本验证

[英]Java Script Validation for file uploading

I got stuck in JavaScript validation in product adding form. 我陷入产品添加形式的JavaScript验证中。

In that page I have file upload control to upload product image. 在该页面中,我具有文件上传控件以上传产品图片。 I am not getting how to validate that using JavaScript. 我没有得到如何使用JavaScript进行验证。

If image is not uploaded to that control I want to display Upload Image message in Label. 如果没有将图像上传到该控件,我想在“标签”中显示“ Upload Image消息。

How to accomplish this? 如何做到这一点? Please help me. 请帮我。

The script I have written is: 我写的脚本是:

var fileup = document.getElementById('<%=FileUploadImg.ClientID %>').value;
if (fileup == "") 
{
   document.getElementById("lblFileUploadImg").innerHTML = "<font color='red'>
   Upload Image File</font>";

   document.getElementById('<%=FileUploadImg.ClientID %>').focus();
   return false;
} 
else 
{
    document.getElementById("lblFileUploadImg").innerHTML = "";
}

The control I have used is: 我使用的控件是:

 <asp:FileUpload ID="FileUploadImg" runat="server" Width="217px" Height="20px" />
 <asp:Label ID="lblFileUploadImg" runat="server" > 

With jQuery you could simply do this: 使用jQuery,您可以简单地做到这一点:

$('#myFile').bind('change', function() {
    if(this.files[0].size>...){
        alert('File is too big');
    };

});

maybe this is what you are looking for: 也许这就是您想要的:

$("input:file").change(function () {
    if ($(this).val() !== "") {
        var ul_file = $(this).val();
        var extension = ul_file.substr((ul_file.lastIndexOf('.') + 1));
        var accepted_file_endings = ["jpg", "jpeg", "bmp", "gif", "tif", "png"];
        extension = extension.toLowerCase()
        if ($.inArray(extension, accepted_file_endings) !== -1) {
    ...

You can get validate file uploading using JavaScript like this. 您可以像这样使用JavaScript获取验证文件上传。

<script type="text/javascript">
        function validate() {
            var uploadcontrol = document.getElementById('<%=FileUploadImg.ClientID%>').value;

            //Regular Expression for fileupload control.
            var reg = /(.doc|.docx|.pdf)$/i;

            if (uploadcontrol.length > 0)
            {
                //Checks with the control value.
                if (reg.test(uploadcontrol))
                {
                    document.getElementById('<%=lblFileUploadImg.ClientID%>').innerHTML = "<font color='green'>Upload Image File</font>";
                    return true;
                }
                else 
                {
                    //If the condition not satisfied shows error message.
                    document.getElementById('<%=lblFileUploadImg.ClientID%>').innerHTML = "Error while upload image";
                    return false;
                }
            }
        } //End of function validate.
    </script>


<asp:FileUpload ID="FileUploadImg" runat="server" Width="217px" Height="20px" />
<asp:Label ID="lblFileUploadImg" runat="server" />
<asp:Button runat="server" Text="Upload" ID="btnupload" onclientclick="return validate();"  />
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script>
        function Validate() {
            var fileup = document.getElementById('<%=FileUploadImg.ClientID %>').value;
            if (fileup == "") {
                document.getElementById('<%=lblFileUploadImg.ClientID%>').innerHTML = "<font color='red'>Upload Image File</font>";

                document.getElementById('<%=FileUploadImg.ClientID %>').focus();
                return false;
            }
            else {
                document.getElementById('<%=lblFileUploadImg.ClientID%>').innerHTML = "";
                return true;
            }
        }
    </script>
</head>

<body>
    <form id="form1" runat="server">
    <div>
        <asp:FileUpload ID="FileUploadImg" runat="server" />
        <asp:Label ID="lblFileUploadImg" runat="server" Text="Label"></asp:Label>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return Validate();" />
    </div>
    </form>
</body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM