Form Validation in Vue.js

From the Vue.js cheat sheet · Forms & v-model · verified Jul 2026

Form Validation

Implementing form validation

vue
<template>
  <form @submit.prevent="validateAndSubmit">
    <div>
      <input 
        v-model="form.email"
        @blur="validateEmail"
        :class="{ error: errors.email }"
      />
      <span v-if="errors.email">{{ errors.email }}</span>
    </div>
    
    <button :disabled="!isValid">Submit</button>
  </form>
</template>

<script setup>
import { reactive, computed } from 'vue'

const form = reactive({
  email: '',
  password: ''
})

const errors = reactive({
  email: '',
  password: ''
})

const isValid = computed(() => 
  !errors.email && !errors.password && 
  form.email && form.password
)

function validateEmail() {
  if (!form.email) {
    errors.email = 'Email is required'
  } else if (!/\S+@\S+\.\S+/.test(form.email)) {
    errors.email = 'Email is invalid'
  } else {
    errors.email = ''
  }
}
</script>
💡 Validate on blur for better UX, clear errors on input
⚡ Use computed properties for dynamic validation states
📌 Consider validation libraries like Vuelidate or VeeValidate
🔥 Always validate on both client and server side

More Vue.js tasks

Back to the full Vue.js cheat sheet