Custom validity helpers for Vue.js and Svelte


Custom validity helpers for Vue.js and Svelte

HTML5 comes with a nifty feature to show custom form validation messages on form submission (see cover image). This looks different in each browser/OS - but it is a standard HTML5 feature, fully supported in all modern browsers.

This is easy enough to use through javascript:

element.setCustomValidity('That is the wrong name!');

But when using a front end framework like Vue.js or Svelte, this means that we first need to get a reference to the DOM element and make sure that the element is mounted before executing above javascript.

Wouldn't it be nice if this could just be set as an attribute directly on each input element? Something like this:

<input validity="That is the wrong name!" />

Here's how you can do just that:


Run before loading your app, to make this globally available:

Vue.directive('validity', function (el, binding) {

In a Vue component (.vue file):

  <input type="text"
         v-validity="name!=='joe'?'That is the wrong name!':''" />

  export default {
    data: function () {
      return {
        name: ''


In a "shared.js" file:

export function Validity(node, val) {
    if(!!val) node.setCustomValidity(val);
    return {
        update(newVal) {

In a ".svelte" component file:

<input type="text"
       use:Validity={name!=='joe'?'That is the wrong name!':''}>

  import {Validity} from './shared.js';
  let name='';

Jesper Høy's
Dev Blog

  • Home (blog posts)
  • About me and this website
  • My developer tech stack
  • My favorite software
  • My favorite online services
  • Cool stuff
  • My side projects
  • Referral codes
  • Our wonderful ice horses