• element ui 表单验证写法


      <el-form ref="form" :model="userInfo" label-width="100px" :rules="rules">
        <el-row>
          <el-col :span="12">
            <el-form-item label="登陆账号:" prop="loginId">
              <el-input v-model="userInfo.loginId" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="姓名:" prop="username">
              <el-input v-model="userInfo.username" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="状态:" prop="enabled">
              <el-select v-model="userInfo.enabled" style=" 100%;">
                <el-option
                  v-for="item in stateList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="手机号:" prop="mobile">
              <el-input v-model="userInfo.mobile" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="邮箱:" prop="email">
              <el-input v-model="userInfo.email" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <div class="btn-group">
              <el-button type="primary" @click="validateForm('form')">保存</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      
      
    let validatePhone = (rule, value, callback) => {
      if (value&&value.trim()&&!/^1[34578]d{9}$/.test(value)) {
        callback(new Error("请输入正确的手机号"));
      } else {
        callback();
      }
    };
    
    let validateEmail = (rule, value, callback) => {
      if (value&&value.trim()&&!/^w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*$/.test(value)) {
        callback(new Error("请输入正确的邮箱号"));
      } else {
        callback();
      }
    };
    
    export default {
      data() {
        return {
          userInfo: {
            loginId:'',
            username:'',
            mobile:'',
            email:'',
          },
          rules: {
            loginId: [{ required: true, message: "请输入登录账号" }],
            username: [{ required: true, message: "请输入用户名" }],
            mobile: [{ validator: validatePhone }],
            email:[{ validator: validateEmail }],
            enabled:[{ required: true, message: "请选择状态" }],
          }
        };
      },
      methods: {
        validateForm(formName) {
          this.$refs[formName].validate(async valid => {
            if (valid) {
            //后台提交
              await this.save();
            } else {
              return false;
            }
          });
        },
      }
    };

     

  • 相关阅读:
    idea.vmoption文件修改之后,Idea无法打开的问题
    py学习:namedtuple 具名元组
    py学习:可变对象作为函数参数默认值
    图解Python变量与赋值(转)
    github 提交的认证方式
    让 IDEA 忽略某个文件夹的方式
    在 Windows 上开启 telnet 功能
    Java原生日志 Java.util.logging
    转:Python简史
    Maven无法下载fastdfs-client-java依赖,Dependency 'org.csource:fastdfs-client-java:1.27-SNAPSHOT' not found.
  • 原文地址:https://www.cnblogs.com/llcdbk/p/12786974.html
Copyright © 2020-2023  润新知