0

I simply validate one input file and get the file name. but I can not do it with second one.

<div id="app">
<form action="#">
<label class="btn btn-xs btn-primary">
    <input type="file" name="pic1" id="12" @change="onFileChangePic" multiple/>
    Upload file
</label>
{{fileName}}
<div><input type="submit" value="submit" :disabled="vvv == false"></div>
</form>
</div>

<script>
const app = new Vue({
  el: '#app',
  data: {
    fileName:null,
    vvv:false
  },
  methods:{
      onFileChangePic(event){
         var fileData =  event.target.files[0];
         this.fileName=fileData.name;

         if(fileData.type == 'application/pdf'){
            this.vvv = true
         }else{
            this.vvv = false
         }
         console.log(event);
      }
  }
})
</script>

i want to add

<input type="file" name="pic2" id="13" @change="onFileChangePic" multiple/>

how can I validate the second input too?

2
  • event.target.files[1]? Commented Dec 4, 2019 at 9:15
  • I tried it. it didn't work Commented Dec 4, 2019 at 9:21

1 Answer 1

1
methods:{
      onFileChangePic(event){
        let isGoodToGo = true
        let files = event.target.files
        for (let i=0; i<files.length; i++) {
            let file = files[i]
          if(file.type != 'application/pdf'){
            isGoodToGo = false
         }
        }
        this.vvv = isGoodToGo
      }
  }

Fiddle link: https://jsfiddle.net/shivampesitbng/k3h1x0jq/11/

Loop through all the files to check its type for validation.

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

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.