Search code examples
vue.jsfilevalidationupload

How can I make an input allowing only certain Names?


Hello guys I have the following code input :

 <v-file-input
      class="mx-6"
      show-size
      accept=".xlsx"
      label="File input"
      @change="selectFile"
    ></v-file-input>

How can I make it accept only specific names.xlsx? Like it will accept xlsx files containing specific words in It.

Example:
name-george.xlsx true
name-anna.xlsx false
george-surname.xlsx true

I want It to contain george word inside the xlsx File Name


Solution

  • use a validation rule. Here is an example to get you started

    https://codepen.io/radvic/pen/XWYxKpQ

    new Vue({
      el: '#app',
      vuetify: new Vuetify(),
      data: () => ({
        rules: [
          v => {
            for(index in v){
              const fileName = v[index].name;
               if(fileName.match(/george(.*)\.xlsx/g) === null){   
                 return 'The file name "'+fileName+'" is wrong';
               }
            }
            return true;          
          },
        ],
      }),
    })
    <script src="https://cdn.jsdelivr.net/npm/babel-polyfill/dist/polyfill.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.x/dist/vue.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/vuetify@2.0.18/dist/vuetify.min.js"></script>
    <div id="app">
      <v-app id="inspire">
        <v-file-input multiple :rules="rules" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"></v-file-input>
      </v-app>
    </div>