0

<script type="text/javascript" language="javascript">
function checkfile(sender) {
    var validExts = new Array(".jpg", ".png", ".gif");
    var fileExt = sender.value;
    fileExt = fileExt.substring(fileExt.lastIndexOf('.'));
    if (validExts.indexOf(fileExt) < 0) {
      alert("Invalid file selected, valid files are of " +
               validExts.toString() + " types.");
      return false;
    
    }
    else return true;
}
</script>
<input type="file" name="attachFile" id="file" onchange="checkfile(this);"  accept="image/*"  onBlur="return chechExtension();"  required >

I used below javascript code for validation of throwing error when user enters invalid file.

Its working as it throws error whenever user uploads files other than jpg,gif,png but it just throws error.& does'nt clear the input file field. so the invalid file is uploaded in Input File.

Whats should I do to clear the field after error id user uploads invalid file.

2 Answers 2

2

Use document.getElementById("file").value = "" to reset the value of the input back to nothing.

Sign up to request clarification or add additional context in comments.

Comments

0

if you are using jquery, you can do the following thing:

    var $copy = $('#file');
    $('#file').remove();
    $('.wrapper').append($copy);//$('.wrapper') is the parent DOM element of input

OR you can put this 'input' element in a "form" element. Like this:

   <form id="myform"><input ....></form>

    document.getElementById("myform").reset(); //you can reset your input button in this way

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.