验证阵列文件字段的表单提交

时间:2013-11-26 06:19:30

标签: javascript jquery html

我正在使用来自http://www.fyneworks.com/jquery/multiple-file-upload/的jquery多文件上传插件。我想验证来自提交。我想确保作为数组的文件字段不应为空我尝试了下面的代码,但是无法正常工作。

function validateForm()
{

var x=document.forms["classifieds"]["file[]"].length;
if(x ==0)
{ 
  alert('Please select a file');
  return false;
}
}

请帮忙。

1 个答案:

答案 0 :(得分:0)

我也尝试使用插件使用多个文件上传,但是没有适当的文档任何这样的预定义方法。 但是在您的情况下,不要检查 0 ,请尝试使用 undefined 进行检查。

function validateForm() {
    var x = document.forms["classifieds"]["file[]"].length;
    if (x == undefined) {
        alert('Please select a file');
        return false;
    }
}

以下是我使用表单提交的不同条件:

1)不上传任何文件,

document.forms["classifieds"]["file[]"].length  //returns undefined

2)有些文件

one file uploaded   // returns 2
two files  uploaded // returns 3
and so on

这是fiddle

希望你理解。