Vue項目的表單校驗實戰(zhàn)指南
一、表單檢驗
1.作用:前端校驗可以省去一些錯誤的請求提交,為后端節(jié)省接口壓力
2.流程:
表單數(shù)據(jù) =》 前端校驗(過濾錯誤請求) =》 后端查詢是否匹配
3.工具:ElementPlus表單組件。https://element-plus.org/zh-CN/component/form.html
- el-form(綁定表單和規(guī)則對象)
- el-form-item(綁定使用的規(guī)則字段)
- el-input(雙向綁定表單數(shù)據(jù))
當(dāng)功能很復(fù)雜的時候,通過多個組件各自負責(zé)某個小功能,再組合成一個大功能
二、步驟
- 按照接口字段準備表單對象并綁定
- 按照產(chǎn)品要求準備規(guī)則對象并綁定
- 指定表單域的校驗字段名
- 把表單對象進行雙向綁定
用戶名:不能為空,字段名為account
密碼:不能為空且為6-14個字符,字段名為password
同意協(xié)議:勾選,字段名為agree
三、前期準備
在src文件夾下的views文件夾下創(chuàng)建一個Login文件夾,然后創(chuàng)建一個Index.vue文件,寫入以下代碼:
<script setup>
import { RouterLink } from 'vue-router';
import { ref } from 'vue';
</script>
<template>
<div>
<header class="login-header">
<div class="container m-top-20">
<h1 class="logo">
<RouterLink to="/">小兔鮮</RouterLink>
</h1>
<RouterLink class="entry" to="/">
進入網(wǎng)站首頁
<i class="iconfont icon-angle-right"></i>
<i class="iconfont icon-angle-right"></i>
</RouterLink>
</div>
</header>
<section class="login-section">
<div class="wrapper">
<nav>
<a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >賬戶登錄</a>
</nav>
<div class="account-box">
<div class="form">
<el-form label-position="right" label-width="60px"
status-icon>
<el-form-item label="賬戶">
<el-input/>
</el-form-item>
<el-form-item label="密碼">
<el-input/>
</el-form-item>
<el-form-item label-width="22px">
<el-checkbox size="large">
我已同意隱私條款和服務(wù)條款
</el-checkbox>
</el-form-item>
<el-button size="large" class="subBtn">點擊登錄</el-button>
</el-form>
</div>
</div>
</div>
</section>
<footer class="login-footer">
<div class="container">
<p>
<a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >關(guān)于我們</a>
<a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >幫助中心</a>
<a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >售后服務(wù)</a>
<a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >配送與驗收</a>
<a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >商務(wù)合作</a>
<a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >搜索推薦</a>
<a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >友情鏈接</a>
</p>
<p>CopyRight © 小兔鮮兒</p>
</div>
</footer>
</div>
</template>
<style scoped lang='scss'>
.login-header {
background: #fff;
border-bottom: 1px solid #e4e4e4;
.container {
display: flex;
align-items: flex-end;
justify-content: space-between;
}
.logo {
width: 200px;
a {
display: block;
height: 132px;
width: 100%;
text-indent: -9999px;
background: url("@/assets/images/logo.png") no-repeat center 18px / contain;
}
}
.sub {
flex: 1;
font-size: 24px;
font-weight: normal;
margin-bottom: 38px;
margin-left: 20px;
color: #666;
}
.entry {
width: 120px;
margin-bottom: 38px;
font-size: 16px;
i {
font-size: 14px;
color: $xtxColor;
letter-spacing: -5px;
}
}
}
.login-section {
background: url('@/assets/images/login-bg.png') no-repeat center / cover;
height: 488px;
position: relative;
.wrapper {
width: 380px;
background: #fff;
position: absolute;
left: 50%;
top: 54px;
transform: translate3d(100px, 0, 0);
box-shadow: 0 0 10px rgba(0, 0, 0, 0.15);
nav {
font-size: 14px;
height: 55px;
margin-bottom: 20px;
border-bottom: 1px solid #f5f5f5;
display: flex;
padding: 0 40px;
text-align: right;
align-items: center;
a {
flex: 1;
line-height: 1;
display: inline-block;
font-size: 18px;
position: relative;
text-align: center;
}
}
}
}
.login-footer {
padding: 30px 0 50px;
background: #fff;
p {
text-align: center;
color: #999;
padding-top: 20px;
a {
line-height: 1;
padding: 0 10px;
color: #999;
display: inline-block;
~a {
border-left: 1px solid #ccc;
}
}
}
}
.account-box {
.toggle {
padding: 15px 40px;
text-align: right;
a {
color: $xtxColor;
i {
font-size: 14px;
}
}
}
.form {
padding: 0 20px 20px 20px;
&-item {
margin-bottom: 28px;
.input {
position: relative;
height: 36px;
>i {
width: 34px;
height: 34px;
background: #cfcdcd;
color: #fff;
position: absolute;
left: 1px;
top: 1px;
text-align: center;
line-height: 34px;
font-size: 18px;
}
input {
padding-left: 44px;
border: 1px solid #cfcdcd;
height: 36px;
line-height: 36px;
width: 100%;
&.error {
border-color: $priceColor;
}
&.active,
&:focus {
border-color: $xtxColor;
}
}
.code {
position: absolute;
right: 1px;
top: 1px;
text-align: center;
line-height: 34px;
font-size: 14px;
background: #f5f5f5;
color: #666;
width: 90px;
height: 34px;
cursor: pointer;
}
}
>.error {
position: absolute;
font-size: 12px;
line-height: 28px;
color: $priceColor;
i {
font-size: 14px;
margin-right: 2px;
}
}
}
.agree {
a {
color: #069;
}
}
.btn {
display: block;
width: 100%;
height: 40px;
color: #fff;
text-align: center;
line-height: 40px;
background: $xtxColor;
&.disabled {
background: #cfcdcd;
}
}
}
.action {
padding: 20px 40px;
display: flex;
justify-content: space-between;
align-items: center;
.url {
a {
color: #999;
margin-left: 10px;
}
}
}
}
.subBtn {
background: $xtxColor;
width: 100%;
color: #fff;
}
</style>效果圖:

四、實戰(zhàn)
(1)賬號和密碼的表單驗證
1.按照接口字段準備表單對象并綁定
(1)先準備好表單對象
// 1.準備表單對象
const form =ref({
account: '',
password: ''
})(2)接著做一個綁定,對el-form組件進行一個綁定 :model
<el-form :model="form" label-position="right" label-width="60px" status-icon>
2.按照產(chǎn)品要求準備規(guī)則對象并綁定
(1)先準備規(guī)則對象
// 2.準備規(guī)則對象
const rules = {
account : [
{required:true , message:'用戶名不能為空!' , trigger :'blur'}
],
password : [
{required: true , message:'密碼不能為空!' , trigger :'blur'},
{min :6 ,max : 14 , message:'密碼長度是6-14位!' , trigger :'blur'},
]
}
- required: true表示用戶名(密碼)是必須的,不能為空。
- message: 是在驗證失敗時顯示的錯誤消息。
- trigger: 'blur'表示當(dāng)用戶離開輸入框(失去焦點)時觸發(fā)驗證。
- min:最小長度
- max:最大長度
(2)對el-form進行規(guī)則對象綁定
<el-form :model="form" :rules="rules" label-position="right" label-width="60px" status-icon>
3.指定表單域的校驗字段名
給el-form-item添加校驗字段名prop=" "
<el-form-item prop="account" label="賬戶">
<el-input/>
</el-form-item>
<el-form-item prop="password" label="密碼">
<el-input/>
</el-form-item>4.把表單對象進行雙向綁定
對el-input進行雙向綁定v-model
<el-input v-model="form.account"/>
<el-input v-model="form.password"/>
5.驗證看效果

我們可以發(fā)現(xiàn),我們之前設(shè)置的驗證規(guī)則都生效的,賬號密碼都不能為空,它們前面都帶有星號
(2)同意協(xié)議的表單驗證
使用自定義校驗規(guī)則,舉個栗子

其中三個參數(shù)分別代表:
- rule:驗證的規(guī)則。
- value:當(dāng)前輸入的數(shù)據(jù)。
- callback:一個回調(diào)函數(shù),用于在驗證完成后執(zhí)行。如果驗證通過,通常會調(diào)用
callback();如果驗證失敗,可能會調(diào)用callback(new Error('錯誤信息'))。
同意協(xié)議的校驗邏輯:如果勾選了,通過校驗,反之。
同樣是我們的四個步驟走:
- 按照接口字段準備表單對象并綁定
// 1.準備表單對象
const form =ref({
account: '',
password: '',
agree: true
})因為在上面我們已經(jīng)對表單對象進行綁定了,這里就不需要了
<el-form :model="form" :rules="rules" label-position="right" label-width="60px" status-icon>?
按照產(chǎn)品要求準備規(guī)則對象并綁定
// 2.準備規(guī)則對象
const rules = {
account : [
{required:true , message:'用戶名不能為空!' , trigger :'blur'}
],
password : [
{required: true , message:'密碼不能為空!' , trigger :'blur'},
{min :6 ,max : 14 , message:'密碼長度是6-14位!' , trigger :'blur'},
],
agree : [
{
validator:(rule,value,callback) => {
console.log(value);
// 勾選就通過,不勾選就不通過
if(value){
callback()
}else{
callback(new Error('請勾選協(xié)議!'))
}
}
}
]
}指定表單域的校驗字段名
<el-form-item prop="agree" label-width="22px">
把表單對象進行雙向綁定
<el-checkbox v-model="form.agree" size="large">
看效果:

(3)整個表單內(nèi)容驗證
問題:每個表單域都有自己的檢驗觸發(fā)事件,如果用戶一上來就點擊登錄怎么辦?
解決:在點擊登錄時需要對所有需要檢驗的表單進行統(tǒng)一檢驗。
步驟:
獲取表單實例(先獲取一個form實例,然后對其綁定)
// 3.獲取form實例做同意校驗 const formRef = ref(null)
<el-form ref="formRef" :model="form" :rules="rules" label-position="right" label-width="60px" status-icon>
調(diào)用實例方法(先給登錄按鈕綁定一個doLogin方法,然后再去定義這個方法)
總結(jié)
到此這篇關(guān)于Vue項目表單校驗的文章就介紹到這了,更多相關(guān)Vue項目表單校驗內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue3響應(yīng)式高階用法之shallowRef()的使用
在Vue3框架中,shallowRef()為開發(fā)者提供了細粒度的響應(yīng)式控制能力,特別適用于處理深層嵌套對象或需要部分響應(yīng)式的場景,本文就來詳細的介紹一下,感興趣的可以了解一下2024-09-09
vue3使用element-plus再次封裝table組件的基本步驟
這篇文章主要介紹了vue3使用element-plus再次封裝table組件的基本步驟,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-03-03
Vue.js結(jié)合bootstrap實現(xiàn)分頁控件
這篇文章主要為大家詳細介紹了Vue.js 合bootstrap實現(xiàn)分頁控件的相關(guān)資料,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03
Vite中自制mock服務(wù)器(不使用第三方服務(wù))
本文主要介紹了Vite中自制mock服務(wù)器,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04

