如何进行文件验证

How to make file validation

本文关键字:验证 文件 何进行      更新时间:2023-09-26

我已经创建了这个脚本:

function validate(f)
    {
    var fname="txt_nama";
    var fld=document.getElementById(fname);
    var fup1 = document.getElementById('file_id');
    var fileName1 = fup1.value;
    var ext1 = fileName1.substring(fileName1.lastIndexOf('.') + 1);
    var file1 = fup1.files;
    var fup2 = document.getElementById('file_foto');
    var fileName2 = fup2.value;
    var ext2 = fileName2.substring(fileName2.lastIndexOf('.') + 1);
    var file2 = fup2.files;
    var email_re = /[a-z0-9!#$%&'*+'/=?^_`{|}~-]+(?:'.[a-z0-9!#$%&'*+'/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?'.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/i;
    if(fld.value == "")
            {      
            alert("Please enter your name");
            fld.focus();
            return false;
            }      

    else if(f.txt_email.value == "")
            {
            alert("Please enter your email address.");
            f.txt_email.focus();
            return false;                                  
            }
    else if (!email_re.test(f.txt_email.value))
            {
            alert("Invalid E-mail address.");
            f.txt_email.focus();
            return false;
            }      
    else if(fup1.value == "")
            {      
            alert("Please enter your Scan identity");
            fup1.focus();
            return false;
            }      
    else if(ext1 != 'jpg' || ext1 != 'GIF' || ext1 != 'JPEG' || ext1 != 'jpeg' || ext1 != 'gif' || ext1 !=       'JPG' || ext1 != 'png' || ext1 != 'PNG')
            {
            alert("Upload images with extention JPG, PNG, or GIF only");
            fup1.focus();
            return false;
            }
    else if(file1.size>200000)
            {
            alert("Max size of Scan data is 200Kb");
            fup1.focus();
            return false;                                                  
            }
    else if(fup2.value == "")
            {      
            alert("Please enter your Photo");
            fup2.focus();
            return false;
            }
    else if(ext2 != 'jpg' || ext2 != 'GIF' || ext2 != 'JPEG' || ext2 != 'jpeg' || ext2 != 'gif' || ext2 != 'JPG' || ext2 != 'png' || ext2 != 'PNG')
            {
            alert("Upload images with extention JPG, PNG, or GIF only");
            fup2.focus();
            return false;
            }
    else if(file2.size>200000)
            {
            alert("Max size of Scan data is 200Kb");
            fup2.focus();
            return false;                                          
            }      

    else{
            return true;}
    }

我在文件上传验证中遇到问题,当我输入文件的正确扩展名时,它仍然是错误的或仍在验证,我在if else中犯了什么错误吗?

有人能帮我吗?

在测试扩展时,应该使用AND,而不是OR:

else if(ext1 != 'jpg' && ext1 != 'GIF' && ext1 != 'JPEG' && ext1 != 'jpeg' && ext1 != 'gif' && ext1 !=  'JPG' && ext1 != 'png' && ext1 != 'PNG')

如果任何一个参数为真,则OR为真。因此,如果扩展是jpg,那么ext1 != 'GIF'将为真,因此整个测试将为真。

正如Barmar所说,您的条件是错误的。

以后,请尝试调试代码。要么在浏览器的调试器中运行它(请参阅手册/docs如何启动它),要么在以下几个地方添加console.log()

        var fup1 = document.getElementById('file_id');
        var fileName1 = fup1.value;
        var ext1 = fileName1.substring(fileName1.lastIndexOf('.') + 1);
        var file1 = fup1.files;
        console.log([fup1, fileName1, ext1]);

这将把对象放在浏览器的控制台上,在那里你可以检查它们,看看它们是否包含你想要的内容。