Supermode
Supermode

Reputation: 936

input type validation javascript

How i can validate file type??. The following error throw an exception "theFileElement is not found"

@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>

Upvotes: 1

Views: 492

Answers (1)

TinyPinkSaxophone
TinyPinkSaxophone

Reputation: 130

theFileElement is not defined anywhere. add

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

before

filename = theFileElement.value;

Also edit your file input to look like

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

Upvotes: 1

Related Questions