vue-validate is a lightweight validation mixin, by using built-in HTML5 form validation features.
You have to set name
attribute for form elements, you could use like this demo.
<form v-validate="optionalValue">
...
</form>
var vm = new Vue({
el: '#form',
mixins: [validate(options)],
methods: {
submit: function () {
alert('TODO submit');
}
}
});
By default, Validate elements after each input
event, You can add the data-lazy="true"
attribute to instead sync after change
events:
<input type="email" name="email" data-lazy="true">
Vue instances vm
expose a property errors
and a method valid(selector)
.
min
max
minlength
maxlength
minlength2
至少几个字(两个字母算一个字,一个中文算一个字)maxlength2
Merge one or more methods to options.methods
, returning true if an element is valid.
options = {
// @param bindingValue - The value passed to the directive
rulename: function (value, elem, param, bindingValue) {
return boolean;
}
};
rulename
: The name of the method used to identify it and referencing it; this must be a valid JavaScript identifier of lower case, e.g. rangelength
.value
: the current value of the validated element.elem
: the element to be validated.param
: the value of data-rule-rulename
on the elem
,parameters specified for the method.vm.errors
One or more key/value pairs, the value consists of input name, validity state and message:
{
age: {
state: 'valueMissing',
message: 'Please fill out this field.'
},
email: {
state: 'typeMismatch',
message: "Please include an '@' in the email address. 'a' is missing an '@'."
}
}
A validity state has the following values:
input required, textarea required
); or, more complicated rules for select elements and controls in radio button groups, as specified in their sections.pattern
attribute.maxlength
attribute (input maxlength, textarea maxlength
).minlength
attribute (input minlength, textarea minlength
).min
attribute.max
attribute.step
attribute.vm.valid(selector)
selector
: Element or css selectorReturns true
if the selector has no validity problems, false
otherwise.
https://caniuse.com/#feat=form-validation