simple-vue-regex-directive
v1.1.1
Published
Generate simple regex directives to apply styles to inputs or custom component with a single inner input in Vue
Downloads
12
Maintainers
Readme
simple-vue-regex-directive
Package on NPM Package on GitHub
Simple, no-dependency way to validate textual <input>
fields (including email and number) with Vue directives. This system works on custom components as well, so long as the custom component only has one text input within the custom definition. Has no dependencies, but must be using ES6 for the project (use Babel or comparable, which ships with Vue CLI projects).
Use
JS Use
Globally
//
import Vue from 'vue'
import SimpleVueRegexDirective from 'simple-vue-regex-directive'
// You can name it anything you like, will be usable with: v-regex with this val
Vue.directive('regex', SimpleVueRegexDirective({
// Arbitrary key-value pairs for input's style. See "Locally" for the default.
color: 'red'
}))
Locally:
export default {
name: 'YourComponent',
directives: {
// name within component, usable in component with v-regex with this value
regex: SimpleVueRegexDirective({
// Arbitrary styles to apply on error. This value here is the default.
'box-shadow': '0 0 5px 5px red inset'
})
}
}
HTML Use:
Static value, only numbers
Note quad-backslash and single quotes, to escape to \d
in RegExp
:
<input v-regex="'^\\\\d+$'"/>
Dynamic values, only numbers, as String:
export default {
data () {
return {
stringPattern: '^\\d+$',
regexpPattern: /^\d+$/
}
}
}
<input v-regex="stringPattern"/>
<any-component-with-only-one-inner-input v-regex="regexpPattern"/>
Notes:
- Passing anything but an Object for error styles makes it use the default val
- Only string values can be used as the values in the error styles object