Skip to content

Instantly share code, notes, and snippets.

@c01nd01r
Created November 28, 2016 19:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save c01nd01r/08afd154d18c0a0d99392746339bc064 to your computer and use it in GitHub Desktop.
Save c01nd01r/08afd154d18c0a0d99392746339bc064 to your computer and use it in GitHub Desktop.
v-filter
//https://jsfiddle.net/z57f9yLh/
//@author: https://jsfiddle.net/user/airyman/fiddles/
<template>
<div id="app">
<input type="text" v-filter="'[a-zA-Z]'" v-model="message">
</div>
</template>
<script>
Vue.directive("filter", {
bind: function(el, binding) {
this.inputHandler = function(e) {
var ch = String.fromCharCode(e.which);
var re = new RegExp(binding.value);
if (!ch.match(re)) {
e.preventDefault();
}
};
el.addEventListener("keypress", this.inputHandler);
},
unbind: function(el) {
el.removeEventListener("keypress", this.inputHandler);
},
inputHandler: null
});
new Vue({
el: "#app",
data: {
message: "Ivan"
}
})
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment