您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關Vue中如何使用Validator表單驗證插件,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
基本使用
<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> <h2>調查</h2> <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>
上述就是小編為大家分享的Vue中如何使用Validator表單驗證插件了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。