Vue3實現(xiàn)登錄表單驗證功能
一.實現(xiàn)思路與效果圖
使用async-validator 插件,阿里出品的 antd 和 ElementUI 組件庫中表單校驗?zāi)J(rèn)使用的 async-validator。
效果圖:

二.實現(xiàn)具體過程
npm i async-validator -S
綁定loginForm
const loginForm = reactive({
username: "",
password: "",
});
初始化檢驗規(guī)則和錯誤提示
// 校驗規(guī)則
const descriptor = reactive({
username: {
required: true,
message: "賬號不能為空",
},
password: [
// 多條校驗規(guī)則
{
required: true,
message: "密碼不能為空",
},
{
validator(rule, value, callback) {
value < 12 ? callback(new Error("不能少于12位")) : callback();
},
},
],
});
created里實例化構(gòu)造函數(shù)表示創(chuàng)建一個校驗器,參數(shù)為校驗規(guī)則對象
const validator = reactive(new Schema(descriptor));
定義提交的方法
const submit = () => {
this.clearMessage()
validator.validate(this.form, {
firstFields: true
}).then(() => {
// 校驗通過
console.log('ok')
}).catch(({ errors }) => {
// 校驗未通過
// 顯示錯誤信息
for (let { field, message } of errors)
this.errorMessage[field] = message
})
},clearMessage // 清空校驗錯誤提示
const clearMessage = () => {
for (let key in errorMessage) {
errorMessage[key] = "";
}
};validate方法
驗證器對象的validate方法用于驗證數(shù)據(jù)是否符合驗證規(guī)則。
如驗證一個空對象,是否符合驗證規(guī)則
function(source, [options], callback): Promise
參數(shù):
- source 要驗證的
對象(必選) - [options] 驗證處理選項
對象(可選) - callback 驗證完成時調(diào)用的回調(diào)
函數(shù)(可選)
options的配置
{
suppressWarning: false, // 是否禁止無效值的內(nèi)部警告
first: false, // 是否在第一個規(guī)則驗證錯誤時調(diào)用回調(diào),不再處理其他驗證規(guī)則
firstFields: true // 是否在指定字段的第一個規(guī)則驗證錯誤時調(diào)用回調(diào),不再處理相同字段的驗證規(guī)則,true代表所有字段
}返回值:
validate方法返回一個promise對象,可以用then方法和catch方法綁定成功或失敗的處理
validator.validate({})
.then(() => {
console.log('驗證通過')
})
.catch(({ errors, fields }) => {
console.log('驗證不通過', errors, fields)
})使用v-bind控制輸入框紅色的顯隱藏 :class="{ checkusername: isActiveUsername }"如果為空則顯示紅色
三.完整代碼與效果圖
完整代碼
<template>
<div class="main">
<h3>Vue3表單驗證</h3>
<div class="form-box">
<div class="form-group">
<label class="label">賬號</label>
<input
:class="{ checkusername: isActiveUsername }"
type="text"
v-model="loginForm.username"
class="input"
placeholder="請輸入賬號"
/>
<span class="check-message">{{ errorMessage.username }}</span>
</div>
<div class="form-group">
<label class="label">密碼</label>
<input
:class="{ checkpassword: isActivePassword }"
tyep="password"
v-model="loginForm.password"
type="text"
placeholder="請輸入密碼"
class="input"
/>
<span class="check-message">{{ errorMessage.password }}</span>
</div>
<div class="form-group">
<button class="btn" @click="submit()">保存</button>
</div>
</div>
</div>
</template>
<script setup>
import Schema from "async-validator";
import { reactive,ref } from "vue";
//控制輸入框變紅
const isActiveUsername = ref(false)
const isActivePassword = ref(false)
// 表單對象
const loginForm = reactive({
username: "",
password: "",
});
// 校驗規(guī)則
const descriptor = reactive({
username: {
required: true,
message: "姓名不能為空",
},
password: [
{
required: true,
message: "密碼不能為空",
},
],
});
// 錯誤提示
const errorMessage = reactive({
username: "",
password: "",
});
const validator = reactive(new Schema(descriptor));
const submit = () => {
if (loginForm.username === '') {
isActiveUsername.value = true
}
if (loginForm.password === '') {
isActivePassword.value = true
}
if (loginForm.username != '') {
isActiveUsername.value = false
}
if (loginForm.password != '') {
isActivePassword.value = false
}
clearMessage();
validator
.validate(loginForm, {
firstFields: true,
})
.then(() => {
// 校驗通過
console.log(" 校驗通過,可以發(fā)起請求");
})
.catch(({ errors }) => {
// 校驗未通過
// 顯示錯誤信息
for (let { field, message } of errors) errorMessage[field] = message;
});
};
// 清空校驗錯誤提示
const clearMessage = () => {
for (let key in errorMessage) {
errorMessage[key] = "";
}
};
</script>
<style scoped>
.main {
text-align: center;
}
.btn {
margin: 0;
line-height: 1;
padding: 15px;
height: 30px;
width: 60px;
font-size: 14px;
border-radius: 4px;
color: #fff;
background-color: #2080f0;
white-space: nowrap;
outline: none;
position: relative;
border: none;
display: inline-flex;
flex-wrap: nowrap;
flex-shrink: 0;
align-items: center;
justify-content: center;
user-select: none;
text-align: center;
cursor: pointer;
text-decoration: none;
}
.form-box {
width: 500px;
max-width: 100%;
margin: 0 auto;
padding: 10px;
border: 5px solid rgb(171 174 186);
}
.form-group {
height: 30px;
margin: 10px;
padding: 10px 15px 10px 0;
}
.label {
padding-right: 10px;
padding-left: 10px;
display: inline-block;
box-sizing: border-box;
width: 110px;
text-align: right;
}
.input {
width: calc(100% - 120px);
height: 28px;
}
.check-message {
color: #d03050;
position: relative;
left: -70px;
}
.checkpassword,
.checkusername {
border: 2px solid #d03050 !important ;
}
</style>
參考鏈接:
https://github.com/tmpfs/async-validate
https://www.cnblogs.com/wozho/p/10955525.html
到此這篇關(guān)于Vue3實現(xiàn)登錄表單驗證的文章就介紹到這了,更多相關(guān)vue3表單驗證內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- vue用elementui寫form表單時,在label里添加空格操作
- vue+ElementUI 關(guān)閉對話框清空驗證,清除form表單的操作
- vue elementui el-form rules動態(tài)驗證的實例代碼詳解
- vue elementui form表單驗證的實現(xiàn)
- Vue ElementUI之Form表單驗證遇到的問題
- Vue 清除Form表單校驗信息的解決方法(清除表單驗證上次提示信息)
- Vue3?+?elementplus實現(xiàn)表單驗證+上傳圖片+?防止表單重復(fù)提交功能
- vue表單驗證rules及validator驗證器的使用方法實例
- 詳談vue中的rules表單驗證(常用)
- vue表單驗證自定義驗證規(guī)則詳解
- vue3在table里使用elementUI的form表單驗證的示例代碼
相關(guān)文章
vue中的vue-print-nb如何實現(xiàn)頁面打印
這篇文章主要介紹了vue中的vue-print-nb如何實現(xiàn)頁面打印,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04
vue中v-cloak解決刷新或者加載出現(xiàn)閃爍問題(顯示變量)
這篇文章主要介紹了vue中v-cloak解決刷新或者加載出現(xiàn)閃爍問題(顯示變量) ,需要的朋友可以參考下2018-04-04
在?Vue?項目中如何引用?assets?文件夾中的圖片(方式匯總)
Vue中引用assets文件夾中的圖片有多種方式,在.vue文件的模板部分,使用相對路徑或通過@別名引用圖片,在CSS中,通過相對路徑或@別名引用圖片作為背景,在JavaScript中,通過import語句導(dǎo)入圖片資源,并使用v-bind在模板中綁定顯示,這些方法均可有效管理和引用項目中的圖片資源2024-09-09
Vuejs仿網(wǎng)易云音樂實現(xiàn)聽歌及搜索功能
這篇文章主要介紹了Vuejs仿網(wǎng)易云音樂實現(xiàn)聽歌及搜索功能,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-03-03
vue項目如何設(shè)置全局字體樣式font-family
這篇文章主要介紹了vue項目如何設(shè)置全局字體樣式font-family問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11
vue中el-tree結(jié)合el-switch實現(xiàn)開關(guān)狀態(tài)切換
本文主要介紹了vue中el-tree結(jié)合el-switch實現(xiàn)開關(guān)狀態(tài)切換,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-12-12
Vue中設(shè)置el-select的高度不生效問題及解決方案
文章介紹了如何使用ElementUI框架中的el-select組件,并解決設(shè)置其高度不生效的問題,在Vue2.x中,使用/deep/關(guān)鍵字可以穿透組件作用域修改樣式;在Vue2.6+到Vue3初期,推薦使用::v-deep關(guān)鍵字;在最新的Vue3和構(gòu)建工具中,推薦使用:deep()偽類來代替::v-deep2025-01-01
Vue + element 實現(xiàn)多選框組并保存已選id集合的示例代碼
這篇文章主要介紹了Vue + element 實現(xiàn)多選框組并保存已選id集合,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06

