vue-form-validator
Deadly simple form validation for Vue.js. Features:
- Built in data types, including email, number, domain, url, etc.
- Customizable validation rules.
- Native support for async validation.
Usage
To use vue-form-validator
, you need to install
it to Vue
.
;;Vue;
Then you can use v-validator
directive on form
element:
Username: Password:
var vm = el: '#app' data: userName: '' password: '' ;
The value for v-validator
attribute is required. You can use it to check the validability of the form. For the code above, vm.loginForm.$valid
means whether the form is valid.
Validation rules
Validation rules are attributes added to input
/select
/textarea
elements. For example, <input type="text" v-model="userName" required>
means the input
is required to fill.
required
The required
rule indicates the form control must be filled or checked. Add required
attribute to the form control without attribute value.
minlength
The minlength="x"
rule means the form control must be filled with at least x
characters.
Add validation rule
Adding custom validation rules are simple! Just call VueValidator.addRule
function and provide rule name and validate function.
/* * the validate function has three arguments: * value: the value user filled * input: the form control element * param: the attribute value of using this rule */VueValidator;
To use your customized rule, add the rule name as input's attribute:
Async validation
To add an async validation rule, return a Promise
in the validate function.
VueValidator;
If you have any async rules in a form, the $valid
will be a Promise
instead of a boolean value. You must wait for that Promise
to check the form's validality:
thisregisterForm$valid;
Todo
- Date format validation
- Documentation
- Unit testing
Contribution
Pull requests are welcome.