Search code examples
javascriptvue.jsvuelidate

How can I check file types with Vuelidate?


How can I check file types (png,jpg,jpeg) with Vue.js Vuelidate library?


Solution

  • You can use helpers validator with regex check. More info here

    import { helpers, required } from 'vuelidate/lib/validators'
    
    const imageRule = helpers.regex('image', /\.(gif|jpe?g|tiff|png)$/)
    
    export default {
        data() {
            return {
                image: '',
            }
        },
        validations: {
            image: {
                required,
                imageRule
            },
        },
    }