1. 程式人生 > >validateField方法對部分表單欄位進行校驗

validateField方法對部分表單欄位進行校驗

<template>
  <div class="content-container developer-container dev-register-container">
    <nav-bar
      :address="address"
      :title="title"
      :register="true">
    </nav-bar>
    <div class="steps">
      <el-steps :active="2" align-center>
        <el-step title="開發商資訊"></el-step>
        <el-step title="提交資質"></el-step>
        <el-step title="管理員資訊"></el-step>
      </el-steps>
    </div>
    <div class="form-list">
        <el-form ref="thirdForm" size="medium" label-position="right" :model="thirdForm" :rules="rules" label-width="100px">
            <el-form-item label="管理員姓名" prop="userName" v-if="firstForm.devpType==0">
                <el-input v-model.trim="thirdForm.userName" placeholder="請輸入管理員姓名,最多20字" ></el-input>
            </el-form-item>

            <el-form-item label="管理員賬號" prop="account">
                <el-input v-model.trim="thirdForm.account" placeholder="請輸入管理賬號,最多50字"></el-input>
            </el-form-item>

            <el-form-item label="登入密碼" prop="password"  >
              <el-input id="pwd" v-model.trim="thirdForm.password" type="password" placeholder="請輸入長度為10-20位包含數字字母及其他特殊字元的密碼" minlength="10" maxlength="20">
                <i slot="suffix" class="el-input__icon el-icon-view el-show" @click="showPwd"></i>
              </el-input>
            </el-form-item>

            <el-form-item label="電子郵箱" prop="email">
                <el-input  v-model.trim="thirdForm.email" placeholder="請輸入電子郵箱,最多50字"></el-input>
            </el-form-item>

            <el-form-item label="電話" prop="telephone" v-if="firstForm.devpType==0">
                <el-input  v-model.trim="thirdForm.telephone" placeholder="請輸入電話,最多50字"></el-input>
            </el-form-item>

            <el-form-item label="傳真" prop="fax" v-if="firstForm.devpType==0">
                <el-input  v-model.trim="thirdForm.fax" placeholder="請輸入傳真,最多50字"></el-input>
            </el-form-item>

            <el-form-item label="郵編" prop="postCode" v-if="firstForm.devpType==0">
                <el-input  v-model.trim="thirdForm.postCode" placeholder="請輸入郵編,最多10字" maxlength=10></el-input>
            </el-form-item>

            <el-form-item label="手機號碼" prop="phone" >
                <el-input  v-model.trim="thirdForm.phone" placeholder="請輸入手機號碼" maxlength=11></el-input>
            </el-form-item>

            <el-form-item label="圖片驗證碼" prop="captchaCode" >
                <div class="vertification-area">
                    <el-input  v-model.trim="thirdForm.captchaCode" placeholder="請輸入圖片驗證碼" maxlength="10" style="margin-bottom: 0px; width: calc(100% - 110px); float: left;">
                    </el-input>
                    <img :src="captchaUrl" class="vertification-code" @click="refresh"/>
                  </div>
            </el-form-item>

            <el-form-item label="手機驗證碼" prop="phoneValidation" >
                <div>
                    <el-input  v-model.trim="thirdForm.phoneValidation" placeholder="請輸入手機驗證碼" style="width: calc(100% - 110px);" maxlength="6"></el-input>
                    <el-button v-show="sendAuthCode" @click="sendMsg(thirdForm.phone)" type="primary" class="sendMsg">傳送驗證碼</el-button>
                    <el-button v-show="!sendAuthCode" type="primary" class="sendMsg">{{auth_time}}秒</el-button>
                </div>
            </el-form-item>
            <el-form-item class="btn-wrap">
              <el-button class="previousStep" @click="previousStep()" >上一步</el-button>
              <el-button :disabled="this.sendAuthCode"  type="primary" @click="nextStep('thirdForm')" class="submitButton">提交</el-button>
            </el-form-item>
        </el-form>
    </div>
  </div>
  </template>

<script>
import md5 from '@/utils/MD5';
import api from '@/api/mopApi';
import global from '@/global';
import navBar from '@/components/nav-bar';
import titleBar from './basic/TitleBar';

export default {
  data() {
    return {
      title: '移動運營平臺',
      address: 'title.png',
      sendAuthCode: true, /* 布林值,通過v-show控制顯示‘獲取按鈕’還是‘倒計時’ */
      auth_time: '120', /* 倒計時 計數器 */
      firstForm: {},
      thirdForm: {
        userName: '',
        account: '',
        password: '',
        email: '',
        telephone: '',
        fax: '',
        postCode: '',
        phone: '',
        phoneValidation: '',
        captchaCode: '',
      },
      captchaUrl: `${global.CTX}/captcha`,
      rules: {
        userName: [
          { required: true, message: '請輸入管理員姓名', trigger: 'blur' },
          { min: 1, max: 20, message: '管理員姓名在20字以內', trigger: 'blur' },

        ],
        account: [
          { required: true, message: '請輸入管理員賬號', trigger: 'blur' },
          { min: 1, max: 50, message: '管理員賬號在50字以內', trigger: 'blur' },
          { pattern: /^\S+$/, message: '賬號不允許有空格', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '請輸入長度為10-20位包含數字、字母、特殊字元的密碼', trigger: 'blur' },
          { pattern: /^(?=.*\d)(?=.*[a-zA-Z])(?=.*[\W_]).{10,20}$/, message: '請輸入長度為10-20位包含數字、字母、特殊字元的密碼', trigger: 'blur' },
        ],
        postCode: [
          { max: 10, message: '郵編長度10位以內', trigger: 'blur' },
        ],
        fax: [
          { max: 50, message: '傳真長度50字以內', trigger: 'blur' },
        ],
        email: [
          { required: true, message: '請輸入郵箱', trigger: 'blur' },
          { max: 50, message: '郵箱在50字以內', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9_.-]
[email protected]
[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/, message: '請檢查郵箱格式是否正確', trigger: 'blur' }, ], telephone: [ { max: 50, message: '電話長度在50位以內', trigger: 'blur' }, ], phone: [ { required: true, message: '請輸入手機號碼', trigger: 'blur' }, { pattern: /^((1[3,5,8][0-9])|(14[5,7])|(17[0,5,6,7,8])|(19[7]))\d{8}$/, message: '請檢查手機號是否正確', trigger: 'blur' }, ], phoneValidation: [ { required: true, message: '請輸入手機驗證碼', trigger: 'blur' }, ], captchaCode: [ { required: true, message: '請輸入圖片驗證碼', trigger: 'blur' }, ], }, }; }, components: { titleBar, navBar, }, methods: { // 是否顯示密碼 showPwd() { const input = document.getElementById('pwd'); if (input.type === 'password') { input.type = 'text'; } else if (input.type === 'text') { input.type = 'password'; } }, // 重新整理圖片驗證碼 refresh() { this.captchaUrl = `${global.CTX}/captcha?tm=${Math.random()}`; }, // 倒計時 getAuthCode() { if (this.authTimeTimer) { clearTimeout(this.authTimeTimer); } this.authTimeTimer = setTimeout(() => { this.auth_time -= 1; if (this.auth_time < 0) { this.sendAuthCode = true; clearTimeout(this.authTimeTimer); } else { this.getAuthCode(); } }, 1000); }, // 傳送簡訊 sendMsg(phoneNum) { this.$refs.thirdForm.validateField('phone', (phoneError) => { console.log(`${phoneError}***************************`); if (!phoneError) { this.auth_time = 120; this.sendAuthCode = false; api.sendMsg({ params: { params: { phone: phoneNum, reason: 'developerReg', }, }, }).then(() => { this.getAuthCode(); this.$confirm('驗證碼已傳送至登記手機號上,請查收。', { confirmButtonText: '確定', center: true, }); }).catch((err) => { this.sendAuthCode = true; this.$message({ message: err.response.message, type: 'error', }); }); } }); }, // 驗證登入賬號是否存在 checkDevpUserAccount(account) { api.checkDevpUserAccount({ params: { params: { account, }, }, }).then((data) => { if (data.state === 0) { this.checkCaptcha(); } }).catch((err) => { this.$message({ message: err.response.message, type: 'error', }); }); }, // 圖片驗證碼驗證是否正確 checkCaptcha() { api.getCaptcha({ params: { params: { captchaCode: this.thirdForm.captchaCode, }, }, }).then(() => { this.checkSmsValidCode(); }).catch((err) => { this.$message({ message: err.tip, type: 'error', }); this.refresh(); }); }, // 驗證簡訊驗證碼 checkSmsValidCode() { api.verifySmsValidCode({ params: { params: { phone: this.thirdForm.phone, reason: 'developerReg', validCode: this.thirdForm.phoneValidation, }, }, }).then((data) => { if (data.state === 0) { this.submit(); } }).catch((err) => { this.$message({ message: err.tip, type: 'error', }); }); }, // 上一步 previousStep() { sessionStorage.setItem('needSecondStepCache', '1'); this.$router.push({ name: 'DeveloperSecond' }); }, // 下一步 nextStep(thirdForm) { this.$refs[thirdForm].validate((valid) => { if (valid) { this.checkDevpUserAccount(this.thirdForm.account); } }); }, // 向後臺提交資料 submit() { if (this.firstForm.devpType === '1') { this.thirdForm.userName = this.firstForm.devpNameself; } this.secondForm.cmmiLevel = (this.secondForm.cmmiLevel === '無' ? '' : this.secondForm.cmmiLevel); this.secondForm.isoLevel = (this.secondForm.isoLevel === '無' ? '' : this.secondForm.isoLevel); const passwordTemp = md5(this.thirdForm.password); api.registeredDeveloper({ params: { data: { devpType: this.firstForm.devpType, devpName: this.firstForm.devpName, devpCode: this.firstForm.devpCode, logo: this.firstForm.logo, companyAddress: this.firstForm.companyAddress, companyDescrible: this.firstForm.companyDescrible, companyBusiness: this.firstForm.companyBusiness, identifyPositiveId: this.firstForm.identifyPositiveId, identifyReverseId: this.firstForm.identifyReverseId, employeeCode: this.firstForm.employeeCode, remarks: this.firstForm.remarks, cmmiLevel: this.secondForm.cmmiLevel, isoLevel: this.secondForm.isoLevel, qualificationId: this.secondForm.qualificationId, userName: this.thirdForm.userName, account: this.thirdForm.account, password: passwordTemp, email: this.thirdForm.email, telephone: this.thirdForm.telephone, fax: this.thirdForm.fax, postCode: this.thirdForm.postCode, phone: this.thirdForm.phone, }, }, }).then(() => { this.$router.push({ name: 'ReturnSuccessMsg', params: {}, }); }).catch((err) => { this.$message({ message: err.response.message, type: 'error', }); }); }, }, mounted() { this.firstForm = JSON.parse(sessionStorage.getItem('firstForm')) || {}; this.secondForm = JSON.parse(sessionStorage.getItem('secondForm')) || {}; }, }; </script> <style scoped lang="less"> .developer-container { overflow: hidden; .btn-wrap{ border-top: 1px solid #EBEEF6; padding-top: 30px; font-size: 0; .previousStep{ padding: 10px 28px; } .submitButton{ padding: 10px 35px; } } } .fl { float: left; } .fr { float: right; } .block { float: right; margin-left: -5px; margin-top: 20px; } .disabled { cursor: default; color: #bbb; pointer-events: none; } .vertification-area { height: 36px; .vertification-code { float: left; height: 36px; line-height: 36px; width: 100px; margin-left: 10px; border-radius: 4px; color: #fff; font-size: 24px; text-align: center; font-weight: bolder; cursor: pointer; } } .el-icon-view{ &:hover{ cursor: pointer; } } </style> <style lang="less"> .dev-register-container{ .steps { padding:40px 22%; margin: 48px auto 0; background: #F9FAFC; border-bottom: 1px solid #EBEEF6; @media screen and (max-width: 1660px) { padding: 40px 16%; } @media screen and (max-width: 1280px) { padding: 40px 10%; } .el-step__line{ top: 14px; } .el-step__head { .el-step__icon{ width: 30px; height: 30px; font-size: 14px; border-color: #C0CCDA; color: #C0CCDA; } &.is-process{ .el-step__icon{ color:#fff; background: #C0CCDA; } } &.is-finish{ .el-step__icon{ color:#fff; background: #20A0FF; border-color: #20A0FF; } } } .el-step__title { font-size: 14px; color: #C0CCDA; font-weight: bold; &.is-process{ color: #475669; } &.is-finish{ color: #20A0FF; } } } .form-list { margin: 30px auto 0; max-width: 800px; width: 75%; } .sendMsg{ height:36px; vertical-align:middle; width: 100px; margin-left: 5px; padding: 10px 15px; } } </style>

 

 

 

對於表單中部分欄位的校驗看如下的程式碼,當校驗不通過時phoneError返回值為校驗的提示資訊,當通過時phoneError的值為空,所以if語句中用(!phoneError)表示校驗通過,完整程式碼及頁面展示圖如上。

sendMsg(phoneNum) {
    this.$refs.thirdForm.validateField('phone', (phoneError) => {
        if(!phoneError){
            //當校驗通過時,這裡面寫邏輯程式碼
        }
    })
};