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).
minmaxminlengthmaxlengthminlength2至少几个字(两个字母算一个字,一个中文算一个字)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 ofdata-rule-rulenameon theelem,parameters specified for the method.
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:
- valueMissing:When a control has no value but has a required attribute (
input required, textarea required); or, more complicated rules for select elements and controls in radio button groups, as specified in their sections. - typeMismatch: When a control that allows arbitrary user input has a value that is not in the correct syntax (E-mail, URL).
- patternMismatch: When a control has a value that doesn't satisfy the
patternattribute. - tooLong: When a control has a value that is too long for the form control
maxlengthattribute (input maxlength, textarea maxlength). - tooShort: When a control has a value that is too short for the form control
minlengthattribute (input minlength, textarea minlength). - rangeUnderflow: When a control has a value that is not the empty string and is too low for the
minattribute. - rangeOverflow: When a control has a value that is not the empty string and is too high for the
maxattribute. - stepMismatch: When a control has a value that doesn't fit the rules given by the
stepattribute. - badInput: When a control has incomplete input and the user agent does not think the user ought to be able to submit the form in its current state.
- customError: When a control's custom validity error message (as set by the element's setCustomValidity() method) is not the empty string.
selector: Element or css selector
Returns true if the selector has no validity problems, false otherwise.