输入类型验证javascript

时间:2012-12-04 01:36:01

标签: javascript asp.net-mvc-3 razor

我如何验证文件类型?以下错误引发异常“找不到该文件”

@using (Html.BeginForm("Index", "Data", FormMethod.Post, new { enctype = "multipart/form-data" }))
{

    <input type="file" name="FileUpload" />
    <input type="submit" name="Submit" id="btnSubmit" value="Upload"/>

}


<script type="text/javascript">
  $('#btnSubmit').click(function (e){

            filename = theFileElement.value;
              if (!/\.csv$/i.test(filename)) {
                alert("Please upload csv file only");
                return false;
              }
              return true;

        });

</script>

1 个答案:

答案 0 :(得分:1)

未在任何地方定义theFileElement。 添加

var theFileElement = document.getElementById("FileUpload");

之前

filename = theFileElement.value;

同时编辑文件输入,使其看起来像

<input type="file" name="FileUpload" id="FileUpload" />