vue2移动端使用vee-validate进行表单验证

使用vee-validate时若要使用中文版本提示时,vee-validate的版本需要注意

"vee-validate": "2.0.0-rc.25"

在main.js里添加如下代码

import VeeValidate, { Validator } from ‘vee-validate‘
import CN from ‘vee-validate/dist/locale/zh_CN.js‘
Validator.addLocale(CN)
Vue.use(VeeValidate, {
  locale: ‘zh_CN‘
})

若想修改默认的提示

// 修改默认错误提示
const dictionary = {
  zh_CN: {
    messages: {
      email: () => ‘邮箱格式不正确哦‘,
      required: (val) => {
        let msg = ‘‘
        switch (val) {
          case ‘email‘:
            msg = ‘邮箱‘
            break
          case ‘qq‘:
            msg = ‘qq‘
            break
          default:;
        }
        msg = msg + ‘不能为空哦‘
        return msg
      }
    }
  }
}
Validator.updateDictionary(dictionary)

自定义校验规则如下:

Validator.extend(‘qq‘, {
  messages: {
    zh_CN: field => ‘qq号码输入不正确‘
  },
  validate: value => {
    return /^[1-9][0-9]{4,14}$/.test(value)
  }
})

以上代码写在js里。组件内进行表单验证的代码如下

<template>
  <div class="hello">
    <form @submit.prevent="validateBeforeSubmit">
      <div class="column is-12">
        <label class="label" for="email">Email</label>
        <p :class="{ ‘control‘: true }">
            <input v-validate="‘required|email‘" v-model="email" :class="{‘input‘: true, ‘is-danger‘: errors.has(‘email‘) }" name="email" type="text" placeholder="Email">
            <span v-show="errors.has(‘email‘)" class="help is-danger">{{ errors.first(‘email‘) }}</span>
        </p>
      </div>
      <div class="column is-12">
        <label class="label" for="qq">qq</label>
        <p :class="{ ‘control‘: true }">
            <input v-validate="‘required|qq‘" :class="{‘input‘: true, ‘is-danger‘: errors.has(‘qq‘) }" name="qq" type="text" placeholder="qq">
            <span v-show="errors.has(‘qq‘)" class="help is-danger">{{ errors.first(‘qq‘) }}</span>
        </p>
      </div>
      <div class="column is-12">
          <p class="control">
              <button class="button is-primary" type="submit">Submit</button>
          </p>
      </div>
    </form>
  </div>
</template>

<script>
export default {
  name: ‘HelloWorld‘,
  data () {
    return {
      email: ‘‘,
      qq: ‘‘
    }
  },
  methods: {
    validateBeforeSubmit () {
      this.$validator.validateAll().then((result) => {
        if (result) {
          // eslint-disable-next-line
          alert(‘Form Submitted!‘);
          return
        }

        alert(‘Correct them errors!‘)
      })
    }
  }
}
</script>

原文地址:https://www.cnblogs.com/nanacln/p/8758711.html

时间: 2024-08-03 13:58:10

vue2移动端使用vee-validate进行表单验证的相关文章

jquery.validate.js表单验证

引用jquery封装好的js文件进行表单验证,提高了Web开发的效率.我写了一个验证的实例给大家展示一下. 实例中包含的验证方法还不全面,如果没有大家想要的可以通过 百度搜索关键:jquery.validate.js表单验证帮助文档来进行查阅. 引入的js文件: <script type="text/javascript" src="js/jquery-1.11.2.js"></script> <script type="te

基于Jquery Validate 的表单验证

基于Jquery Validate 的表单验证 jquery.validate.js是jquery下的一个验证插件,运用此插件我们可以很便捷的对表单元素进行格式验证. 在讲述基于Jquery Validate的表单验证前,我们先回顾一下基础纯JS的表单验证. 1.回顾基于JS的表单验证 我们先写一个简单的验证邮箱.手机号的表单,页面代码如下: 1 <form action="XXXX.action" method="post" onsubmit="r

Jquery.validate.js表单验证插件的使用

作为一个网站web开发人员,以前居然不知道还有表单验证这样好呀的插件,还在一行行写表单验证,真是后悔没能早点知道他们的存在. 最近公司不忙,自己学习一些东西的时候,发现了validation的一个实例讲解应用.it's perfect. 首先记录一些使用过程中,爱犯的错误: 1>忘记给表单form添加id属性 2>input这些表单标签必须id属性和name属性名字一样.例如:<input type="text" id="name" name=&q

jQuery.validate.js表单验证插件

jQuery.validate.js表单验证插件的使用 效果: 代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-e

使用validate进行表单验证时土方法(appendTo)改变error显示的位置

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">使用validate进行表单验证时会出现error显示位置非所要求位置的现象,此时的解决方法是:</span> 1.所需要显示error message 的位置定义好无内容的div,并通过设置宽高"占位"(此时可能会用到position:absolute)

jquery validate触发表单验证

jquery validate是常用的表单验证插件.遇到一个jquery validate表单验证不触发的问题 目前写法:$(function(){  $("#form").validate({     rules : {            themeColor: {                required : true            },            --        },        messages : {            themeCol

兼容IE8以下浏览器input表单属性placeholder不能智能提示功能,以及使用jquery.validate.js表单验证插件的问题处理

当前很多表单提示使用了表单属性placeholder,可这属性不兼容IE8以下的浏览器,我自己写了一个兼容处理js // 兼容IE8以下浏览器input不能智能提示功能 if(navigator.appName == "Microsoft Internet Explorer" && (navigator.appVersion.match(/7./i)=="7." || navigator.appVersion.match(/8./i)=="

基于Bootstrap+jQuery.validate Form表单验证实践

项目结构 : github 上源码地址:https://github.com/starzou/front-end-example    点击打开 1.form 表单代码 [html] view plaincopy <!DOCTYPE html> <html> <head> <title>Bootstrap Form Template</title> <meta charset="utf-8" /> <meta

jquery validate.js表单验证的基本用法入门--不用写繁琐的验证代码了...

jquery.validate.js是jquery下的一个验证插件,功能比较强大,早就有所耳闻但是一只没有动手用过,现在在于能够研究一下了. 这里转载一篇前辈写的文章,在我自己的理解上修改了一下,仅作记录. 先贴一个国内某大公司的代码: 1 <script type="text/javascript"> 2 function lang(key) { 3 mylang = { 4 'ls_input_myb': '请输入您的账户', 5 'ls_myb_email': '漫游

jQuery Validate 提交表单验证失败扩展方法

由于Validate没有提供表单提交过后,验证不通过触发方法.这里做一下扩展. 引用场景:每次提交表单元素验证不通过触发方法 打开源代码 找到focusInvalid 方法, 这里是提交表单时验证不通过触发方法,在这里做扩展是就好不 过的. focusInvalid: function() { if ( this.settings.focusInvalid ) { try { $(this.findLastActive() || this.errorList.length && this.