Vue-validator 是Vue的表单考证插件
Vue版本:1.0.24
Vue-validator版本:2.1.3
基础运用
<div id="app">
<validator name="validation">
<form novalidate>
<div class="username-field">
<label for="username">username:</label>
<input type="text" id="username" v-validate:username="['required']" />
</div>
<div class="comment-filed">
<label for="comment">comment:</label>
<input type="text" id="comment" v-validate:comment="{maxlength: 256}" />
</div>
<div class="errors">
<p v-if="$validation.username.required">请输入你的名字</p>
<p v-if="$validation.comment.maxlength">您的批评太长了</p>
</div>
<input type="submit" value="send" v-if="$validation.valid" />
</form>
</validator>
</div>
<script src="//cdn.bootcss.com/vue/1.0.24/vue.js" type="text/javascript" charset="utf-8"></script>
<script src="//cdn.bootcss.com/vue-validator/2.1.3/vue-validator.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
new Vue({
el: '#app'
});
</script>
将要考证的表单包裹在validator自定义元素指令中,而在要考证的表单控件元素的v-validate
属性上绑定响应的校验划定规矩。
考证效果会保存在组建实例的$validation
属性下。$validation
是由validator
元素和name
属性和$
前缀组件
考证效果构造
{
// 表单团体考证
"valid": false, // 字段校验是不是经由过程
"invalid": true, // valid 取反
"touched": false, // 校验字段地点元素取得经由过程核心时返回true,不然返回false
"untouched": true, // touched 取反
"modified": false, // 当元素值与初始值不同时返回true,不然返回false
"dirty": false, // 字段值改变过最少一次返回true,不然返回false
"pristine": true, // dirty 取反
// 字段单一考证
"username": {
"required": true,
"modified": false,
"pristine": true,
"dirty": false,
"untouched": true,
"touched": false,
"invalid": true,
"valid": false
},
"comment": {
"maxlength": false,
"modified": false,
"pristine": true,
"dirty": false,
"untouched": true,
"touched": false,
"invalid": false,
"valid": true
}
}
校验效果由两部分构成。表单团体校验效果和单个字段校验效果。
考证器语法
v-validate
指令语法:
v-validate[:field]=”array literal | object literfal | binding”
校验字段名field
field用来标识校验字段,以后能够用该字段来援用校验效果
v-validate
指令用来定义校验划定规矩,其值可所以数组字面量,对象字面量,组件实例数组属性名。
数组字面量
当校验器不须要分外参数时,能够运用数组字面量情势,如required
校验器,只需出现就带I表该校验器地点元素是必填项。
<div id="app">
<validator name="validation">
<form novalidate>
Zip: <input type="text" v-validate:zip="['required']" /><br />
<div>
<span v-if="$validation.zip.required">邮政编码是必填项</span>
</div>
</form>
</validator>
</div>
对象字面量
对象字面量语法合适须要分外参数的校验器。如限定输入长度的校验器 minlength,须要申明限定长度若干。
<div id="app">
<validator name="validation">
<form novalidate>
ID: <input type="text" v-validate:id="{ required:true, minlength: 3, maxlength: 16 }" />
<br />
<div>
<p v-if="$validation.id.required">ID不能为空</p>
<p v-if="$validation.id.minlength">你的ID名字太短</p>
<p v-if="$validation.id.maxlength">你的ID名字太长</p>
</div>
<input type="submit" value="send" v-if="$validation.valid" />
</form>
</validator>
</div>
还能够用 对象字面量语法经由过程rule
字段来自定义考证划定规矩
<div id="app">
<validator name="validation">
<form novalidate>
ID: <input type="text" v-validate:id="{minlength: {rule: 3}, required: true, maxlength: {rule: 16}}" />
<br />
<div>
<p v-if="$validation.id.required">ID不能为空</p>
<p v-if="$validation.id.minlength">你的ID名字太短</p>
<p v-if="$validation.id.maxlength">你的ID名字太长</p>
</div>
<input type="submit" value="send" v-if="$validation.valid" />
</form>
</validator>
</div>
实例数据属性
v-validate
的值可所以组建实例的数据属性。如许能够用来动态绑定校验划定规矩。
<div id="app">
<validator name="validation">
<form novalidate>
ID: <input type="text" v-validate:id="rules" /><br />
<div>
<p v-if="$validation.id.required">不能为空</p>
<p v-if="$validation.id.minlength">你的ID太短</p>
<p v-if="$validation.id.maxlength">你的ID太长</p>
</div>
</form>
</validator>
</div>
<script src="//cdn.bootcss.com/vue/1.0.24/vue.js" type="text/javascript" charset="utf-8"></script>
<script src="//cdn.bootcss.com/vue-validator/2.1.3/vue-validator.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
new Vue({
el: '#app',
data: {
rules: {
required: true,
minlength: 3,
maxlength: 16
}
}
});
</script>
内置校验划定规矩
vue-validator 内置一些经常使用的考证划定规矩:
required — 输入值不能为空
pattern — 必需婚配pattern示意的正则表达式
minlength — 输入值长度不能小于minlength示意的值
maxlength — 输入的值不能大于maxlength示意的值
min — 输入值不能小于min示意的值
max — 输入值不能大于max示意的值
与v-model同时运用
vue-validator会自动校验经由过程v-model动态设置的值。
<div id="app">
<validator name="validation">
<form novalidate>
message: <input type="text" v-model="msg" v-validate:message="{required: ture, minlength: 8}" />
<br />
<p v-if="$validation.message.required">message不能为空</p>
<p v-if="$validation.message.minlength">message输入太长位数</p>
</form>
</validator>
</div>
<script src="//cdn.bootcss.com/vue/1.0.24/vue.js" type="text/javascript" charset="utf-8"></script>
<script src="//cdn.bootcss.com/vue-validator/2.1.3/vue-validator.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
var vm = new Vue({
el: '#app',
data: {
msg: ''
}
});
setTimeout(function () {
vm.msg = 'hello world!';
}, 2000);
</script>
重置校验效果
经由过程在Vue组件实例上挪用$resetValidation();方法来动态重置校验效果。
<div id="app">
<validator name="validation">
<form novalidate>
<div class="username-field">
<label for="username">username:</label>
<input type="text" id="username" v-validate:username="['required']" />
</div>
<div class="comment-filed">
<label for="comment">comment:</label>
<input type="text" id="comment" v-validate:comment="{maxlength: 256}" />
</div>
<div class="errors">
<p v-if="$validation.username.required">用户名不能为空</p>
<p v-if="$validation.comment.maxlength">输入笔墨凌驾256个</p>
<input type="submit" value="send" v-if="$validation.valid" />
<button type="button" @click="onReset">Reset Validation</button>
</div>
<pre>{{$validation | json}}</pre>
</form>
</validator>
</div>
<script src="//cdn.bootcss.com/vue/1.0.24/vue.js" type="text/javascript" charset="utf-8"></script>
<script src="//cdn.bootcss.com/vue-validator/2.1.3/vue-validator.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
new Vue({
el: '#app',
methods: {
onReset: function () {
this.$resetValidation();
}
}
});
</script>
复选框checkbox
<div id="app">
<validator name="validation">
<form novalidate>
<h1>观察</h1>
<fieldset>
<legend>请挑选生果</legend>
<input type="checkbox" id="apple" value="apple" v-validate:fruits="{
required: { rule: true, message: requiredErrorMsg },
minlength: { rule: 1, message: minlengthErrorMsg },
maxlength: { rule: 2, message: maxlengthErrorMsg }
}" />
<label for="apple">Apple</label>
<input type="checkbox" id="orange" value="orange" v-validate:fruits />
<label for="orange">Orange</label>
<input type="checkbox" id="grape" value="grape" v-validate:fruits />
<label for="grape">Grape</label>
<input type="checkbox" id="banana" value="banana" v-validate:fruits />
<label for="banana">Banana</label>
<ul class="errors">
<li v-for="msg in $validation.fruits">
<p>{{msg | json}}</p>
</li>
</ul>
</fieldset>
</form>
</validator>
</div>
<script src="//cdn.bootcss.com/vue/1.0.24/vue.js" type="text/javascript" charset="utf-8"></script>
<script src="//cdn.bootcss.com/vue-validator/2.1.3/vue-validator.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
new Vue({
el: '#app',
computed: {
requiredErrorMsg: function () {
return '请挑选生果';
},
minlengthErrorMsg: function () {
return '请挑选最少1个生果!';
},
maxlengthErrorMsg: function () {
return '请挑选最多2个生果!';
}
}
});
</script>
下拉类表select
<div id="app">
<validator name="validation">
<form novalidate>
<select v-validate:lang="{required: true}">
<option value="">请挑选言语</option>
<option value="javascript">javascript</option>
<option value="php">php</option>
<option value="node">node</option>
</select>
<div class="errors">
<p v-if="$validation.lang.required">不能为空!</p>
</div>
</form>
</validator>
</div>
<script src="//cdn.bootcss.com/vue/1.0.24/vue.js" type="text/javascript" charset="utf-8"></script>
<script src="//cdn.bootcss.com/vue-validator/2.1.3/vue-validator.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
new Vue({
el: '#app'
});
</script>
校验状况class
各校验状况都有其对应的class(默许) 也能够自定义校验状况class
<validator name="validation" :classes="{touched: 'touehc-validator', dirty: 'dirty-validator'}">
<label for="username">username</label>
<<input type="text" id="username" :classes="{valid: 'valid-username', invalid: 'invalid-username'}" v-validate:username="{required: {rule: true, message: 'required you name!'}}">
</validator>
<!-- classes属性只能在validator元素或应用了v-validate的元素上运用有用 -->
分组校验
vue-validator支撑分组校验。比方反复暗码功用。
<div id="app">
<validator name="validation" :groups="['passwordGroup']">
<form novalidate>
username: <input type="text" v-validate:username="['required']" /><br />
password: <input type="password" v-validate:password="{ minlength: 8, required: true }" group="passwordGroup" /><br />
comfirm password: <input type="password" v-validate:password-comfirm="{minlength: 8, required: true}" group="passwordGroup" />
<div class="errors">
<p v-if="$validation.username.required">用户名不能为空</p>
<p v-if="$validation.password.required">暗码不能为空</p>
<p v-if="$validation.password.minlength">暗码不能少于8位</p>
<p v-if="$validation.password-comfirm.required">反复暗码不能为空</p>
<p v-if="$validation.password-comfirm.minlength">暗码不能少于8位</p>
<p v-if="$validation.passwordGroup.valid">暗码不一致</p>
</div>
</form>
</validator>
</div>
<script src="//cdn.bootcss.com/vue/1.0.24/vue.js" type="text/javascript" charset="utf-8"></script>
<script src="//cdn.bootcss.com/vue-validator/2.1.3/vue-validator.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
new Vue({
el: '#app'
});
</script>