今天小编给大家分享一下vue+vue validator怎么实现表单验证功能的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。
具体代码如下所示:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div id="app"> <validator name="myForm"> <form novalidate> Zip: <input type="text" v-validate:zip="['required']"><br /> <div> <span v-if="$myForm.zip.required">Zip code is required.</span> </div> </form> </validator> </div> <script src="https://unpkg.com/vue@1.0.26/dist/vue.min.js"></script> <script src="https://cdn.bootcss.com/vue-validator/2.1.3/vue-validator.js"></script> <script> new Vue({ el:"#app" }) </script> </body> </html>
2.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>radio button validation example</title> <script src="../../node_modules/vue/dist/vue.js"></script> <script src="../../dist/vue-validator.js"></script> <style> input.invalid { border-color: red; } .errors { color: red; } </style> </head> <body> <div id="app"> <h2>Survey</h2> <validity-group field="fruits" v-model="validation" :validators="{ required: { message: requiredErrorMsg } }"> <legend>Which do you like fruit ?</legend> <input id="apple" type="radio" name="fruit" value="apple" @change="handleValidate" @focusin="handleValidate"> <label for="apple">Apple</label> <input id="orange" type="radio" name="fruit" value="orange" @change="handleValidate" @focusin="handleValidate"> <label for="orange">Orage</label> <input id="grape" type="radio" name="fruit" value="grage" @change="handleValidate" @focusin="handleValidate"> <label for="grape">Grape</label> <input id="banana" type="radio" name="fruit" value="banana" @change="handleValidate" @focusin="handleValidate"> <label for="banana">Banana</label> <ul class="errors"> <li v-for="error in validation.result.errors"> <p :class="error.field + '-' + error.validator">{{error.message}}</p> </li> </ul> </validity-group> </div> <script> new Vue({ data: { validation: { result: {} } }, computed: { requiredErrorMsg: function () { return 'Required fruit !!' } }, methods: { handleValidate: function (e) { var $validity = e.target.$validity $validity.validate() } } }).$mount('#app') </script> </body> </html>
以上就是“vue+vue validator怎么实现表单验证功能”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。