Simple Vue.js Input Validation Plugin – Vee-Validate

vee-validate is a lightweight plugin for Vue.js that allows you to validate input fields, and display errors.

Easy, fast, flexible, progressive enhancement, and multiple languages. Compatible with Vue 3 and Vue 2.

Basic Usage

1. Import and register the Field and Form components.

import { Field, Form } from 'vee-validate';
export default {
  components: {
    Field,
    Form,
  },
  // ...
};

2. Apply a basic Require validator to a form field.

<Form v-slot="{ errors }">
  <Field name="field" :rules="isRequired" />
  <span>{{ errors.field }}</span>
</Form>
export default {
  components: {
    Field,
    Form,
  },
  methods: {
    isRequired(value) {
      return value ? true : 'This field is required';
    },
  },
};

3. You can also use the following composition functions to validate the form.

import { useField } from 'vee-validate';
export default {
  setup() {
    function isRequired(value) {
      if (value && value.trim()) {
        return true;
      }
      return 'This is required';
    }
    const { errorMessage, value } = useField('fieldName', isRequired);
    return {
      errorMessage,
      value,
    };
  },
};
<template>
  <div>
    <input v-model="value" type="text" />
    <span>{{ errorMessage }}</span>
  </div>
</template>
// form-level validation
import { useForm, useField } from 'vee-validate';
export default {
  setup() {
    // Define a validation schema
    const simpleSchema = {
      email(value) {
        // validate email value and return messages...
      },
      name(value) {
        // validate name value and return messages...
      },
    };
    // Create a form context with the validation schema
    useForm({
      validationSchema: simpleSchema,
    });
    // No need to define rules for fields
    const { value: email, errorMessage: emailError } = useField('email');
    const { value: password, errorMessage: passwordError } = useField('password');
    return {
      email,
      emailError,
      password,
      passwordError,
    };
  },
};
<template>
  <div>
    <input name="email" v-model="email" />
    <span>{{ emailError }}</span>
    <input name="password" v-model="password" type="password" />
    <span>{{ passwordError }}</span>
  </div>
</template>

Preview:

vee-validate

Changelog:

v4.5.11 (04/10/2022)

  • Fixed: Ignored validation of fields during unmounting

v4.5.10 (03/08/2022)

  • Fixed an issue with da.json locale which caused the JSON file to not parse correctly

Download Details:

Author: logaretm

Live Demo: https://vee-validate.logaretm.com/v4/examples/checkboxes-and-radio

Download Link: https://github.com/logaretm/vee-validate/archive/master.zip

Official Website: https://github.com/logaretm/vee-validate

Install & Download:

# Yarn
$ yarn add [email protected]

# NPM
$ npm i [email protected] --save

Add Comment