首页 > 其他分享 >vue:自定义validator/验证规则([email protected])

vue:自定义validator/验证规则([email protected])

时间:2023-09-30 16:22:52浏览次数:32  
标签:el vue form 自定义 value element ruleForm callback

一,官方文档地址:

https://element-plus.gitee.io/zh-CN/component/form.html#%E8%87%AA%E5%AE%9A%E4%B9%89%E6%A0%A1%E9%AA%8C%E8%A7%84%E5%88%99

二,js代码:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 <template>   <div class="view_root">   <el-card shadow="never"> <el-form :model="ruleForm" status-icon :rules="rules" ref="refForm" label-width="100px" class="demo-ruleForm" style="width:600px;"> <el-form-item label="原始密码" prop="originPass" style="margin-bottom: 20px;">   <el-input  type="password" v-model="ruleForm.originPass" placeholder="请输入原始密码" autocomplete="off" show-password></el-input> </el-form-item> <el-form-item label="新密码" prop="newPass" style="margin-bottom: 20px;">   <el-input type="password" placeholder="请输入新密码" v-model="ruleForm.newPass" autocomplete="off" show-password></el-input> </el-form-item> <el-form-item label="确认新密码" prop="checkPass" style="margin-bottom: 20px;">   <el-input type="password" placeholder="请再次输入新密码" v-model="ruleForm.checkPass" autocomplete="off" show-password></el-input> </el-form-item> <el-form-item>   <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>   <el-button @click="resetForm('ruleForm')">重置</el-button> </el-form-item> </el-form>   </el-card>   </div> </template> <script> import {ElMessage} from "element-plus"; import {ref,reactive} from "vue"; import {post} from "@/api/axios";   export default {   name:"UserPassword",   setup() {     //表单的ref     const refForm = ref(null);       //提交表单     const submitForm = () => {       refForm.value.validate((valid) => {         if (valid) {           //提交到api           try {       var data = new FormData();       // 创建一个表单数据       data.append("originpass",ruleForm.originPass);       data.append("newpass",ruleForm.newPass);       data.append("checkpass",ruleForm.checkPass);         post('/api/login/password',data).then(res => {         if (res.code == 0) {               //提示               ElMessage.success("密码修改成功!");         } else {           ElMessage.error("密码修改失败:"+res.msg);         }       }).catch((error) => {         console.log(error)       })             } catch (error) {             console.log(error)             ElMessage.error("密码修改出错");           }           } else {           console.log('error submit!!');           return false;         }       });     };         //重置表单     const resetForm = () => {       refForm.value.resetFields();     }       //表单的数据     const ruleForm = reactive({         originPass:'',         newPass: '',         checkPass: '',     });       //原始密码的验证     const validateOriginPass = (rule, value, callback) => {       if (value === '') {         callback(new Error('请输入原始密码'));       } else if (value.length < 3) {         callback(new Error('密码长度应不低于6个字符'));       } else {         callback();       }     };     //新输入密码的验证     const validatePass = (rule, value, callback) => {       if (value === '') {         callback(new Error('请输入新密码'));       } else if (value.length < 6) {         callback(new Error('密码长度应不低于6个字符'));       } else {         if (ruleForm.checkPass !== '') {             refForm.value.validateField('checkPass', () => null);         }         callback();         }     };     //确认密码的验证     const validatePass2 = (rule, value, callback) => {       if (value === '') {         callback(new Error('请再次输入密码'));       } else if (value.length < 6) {         callback(new Error('密码长度应不低于6个字符'));       } else if (value !== ruleForm.newPass) {         callback(new Error('两次输入密码不一致!'));       } else {         callback();       }     };         //表单验证的规则       const rules = ref({         originPass: [           {required: true, validator: validateOriginPass, trigger: 'blur' }         ],         newPass: [           { required: true,validator: validatePass, trigger: 'blur' }         ],         checkPass: [           {required: true, validator: validatePass2, trigger: 'blur' }         ],       });          return {           refForm,           rules,           ruleForm,             resetForm,           submitForm,        }     }, } </script>

三,效果:

说明:刘宏缔的架构森林—专注it技术的博客,
网站:https://blog.imgtouch.com
原文: https://blog.imgtouch.com/index.php/2023/09/29/vue-zi-ding-yi-validator-yan-zheng-gui-ze-elementplus-2-3-12/
代码: https://github.com/liuhongdi/ 或 https://gitee.com/liuhongdi
说明:作者:刘宏缔 邮箱: [email protected]

四,查看vue/element-plus框架版本:

liuhongdi@lhdpc:/data/vue/responsive$ npm list vue
[email protected] /data/vue/responsive
└─┬ [email protected]
  └─┬ @vue/[email protected]
    └── [email protected] deduped
liuhongdi@lhdpc:/data/vue/responsive$ npm list element-plus
[email protected] /data/vue/responsive
└── [email protected]

标签:el,vue,form,自定义,value,element,ruleForm,callback
From: https://www.cnblogs.com/architectforest/p/17737953.html

相关文章

  • vue:el-table在resize时报错([email protected])
    一,报错信息:Uncaughtruntimeerrors:×ERRORResizeObserverloopcompletedwithundeliverednotifications.athandleError(webpack-internal:///./node_modules/webpack-dev-server/client/overlay.js:299:58)ateval(webpack-internal:///./node_modules/webpa......
  • vue处理响应式的思路
    首先看如下js代码leta='张三'console.log(a)//当前页面展示的是张三a='李四'首先页面刚开始渲染的时候数据a为张三,之后将a修改为了李四以后页面不会发生改变,但是数据已经修改了,vue为了解决这一问题,采用响应式的办法。通过对象的defineProperty方法,在回调函数中监听;......
  • Vue自定义指令
    定义指令全局定义//指令名称为:mydirec1Vue.directive('mydirec1',{//指令配置})//指令名称为:mydirec2Vue.directive('mydirec2',{//指令配置})之后,所有的组件均可以使用mydirec1和mydirec2指令<template><!--某个组件代码--><div><MyCompv-......
  • vue_error_Runtime directive used on component with non-element root node. The di
    翻译:'运行时指令,用于非元素根节点的组件。这些指令将无法发挥预期的作用';这个错误发生在我将v-show放在自定义组件上时,我想是因为自定义组件在渲染时会被自定义组件的内部元素替换,因此设置是无效的解决:在自定义组件外加一个div,把v-show写在div上......
  • 基于vue和element-ui开发仿桌面文件夹组件
    ......
  • 【日常收支账本】【Day03】通过ElementTree+XPath实现对XML文件的读写
    一、项目地址https://github.com/LinFeng-BingYi/DailyAccountBook二、新增1.解析xml文件1.1功能详述解析所设计的xml文件格式,并将所得数据存入变量。点击查看xml格式<DailyAccountBook><balance><fund><value>5000.00</value>......
  • vue前端框架ruoyi介绍
    Ruoyi是一个基于Vue.js和SpringBoot框架构建的开源前后端分离的企业级快速开发平台。它遵循了前后端分离的架构模式,将前端和后端进行解耦,使得系统更加灵活、可扩展和易于维护。Ruoyi的前端部分采用了Vue.js框架,这是一个流行的JavaScript前端框架,专注于构建用户界面。Vue......
  • FastAPI学习-24.自定义异常处理器 exception_handler
    前言添加自定义处理器,要使用 Starlette的异常工具。安装自定义异常处理器假设要触发的自定义异常叫作 UnicornException。且需要FastAPI实现全局处理该异常。此时,可以用 @app.exception_handler() 添加自定义异常控制器:fromfastapiimportFastAPI,Requestfromfa......
  • 基于vue和element-ui的搜索下拉滚动条组件
    ......
  • Vue源码学习(八):生命周期调用
    好家伙, Vue源码学习(七):合并生命周期(混入Vue.Mixin)书接上回,在上一篇中,我们已经实现了合并生命周期现在,我们要在我们的初始化过程中,注册生命周期 1.项目目录 红框为本篇涉及到的.js文件 2.先来看/utils/index.jsexportconstHOOKS=["beforeCreated......