欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue3實現(xiàn)登錄表單驗證功能

 更新時間:2022年06月23日 15:09:00   作者:~前端小攻城獅  
這篇文章主要介紹了Vue3實現(xiàn)登錄表單驗證功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

一.實現(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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue回到頂部監(jiān)聽滾動事件詳解

    vue回到頂部監(jiān)聽滾動事件詳解

    這篇文章主要為大家詳細介紹了vue回到頂部監(jiān)聽滾動事件,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-08-08
  • .html頁面引入vue并使用公共組件方式

    .html頁面引入vue并使用公共組件方式

    這篇文章主要介紹了.html頁面引入vue并使用公共組件方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • 在vue使用clipboard.js進行一鍵復(fù)制文本的實現(xiàn)示例

    在vue使用clipboard.js進行一鍵復(fù)制文本的實現(xiàn)示例

    這篇文章主要介紹了在vue使用clipboard.js進行一鍵復(fù)制文本的實現(xiàn)示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-01-01
  • 解讀vue項目防范XSS攻擊問題

    解讀vue項目防范XSS攻擊問題

    這篇文章主要介紹了解讀vue項目防范XSS攻擊問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • Vue前端路由hash與history差異深入了解

    Vue前端路由hash與history差異深入了解

    這篇文章主要為大家介紹了Vue前端路由hash與history差異的深入了解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-06-06
  • 關(guān)于vue3使用particles粒子特效的問題

    關(guān)于vue3使用particles粒子特效的問題

    這篇文章主要介紹了關(guān)于vue3使用particles粒子特效的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • Vue 實現(xiàn)可視化拖拽頁面編輯器

    Vue 實現(xiàn)可視化拖拽頁面編輯器

    這篇文章主要介紹了Vue 實現(xiàn)可視化拖拽頁面編輯器的方法,幫助大家更好的理解和使用vue,感興趣的朋友可以了解下
    2021-02-02
  • Vue做一個簡單的隨機點名冊

    Vue做一個簡單的隨機點名冊

    這篇文章主要介紹的是如何用Vue做一個簡單的隨機點名冊,主要是做個簡單的點名器,不做樣式,需要的朋友可以參考一下,希望對你有所幫助
    2021-12-12
  • 使用vue-print-nb打印el-table問題總結(jié)

    使用vue-print-nb打印el-table問題總結(jié)

    這篇文章主要介紹了使用vue-print-nb打印el-table問題總結(jié),通過實例代碼介紹了vue-print-nb 打印功能,本文結(jié)合實例代碼講解的非常詳細,感興趣的朋友一起看看吧
    2024-01-01
  • Vue項目實現(xiàn)html5圖片上傳的示例代碼

    Vue項目實現(xiàn)html5圖片上傳的示例代碼

    本文主要介紹了Vue項目?html5圖片上傳,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01

最新評論