各种表单验证及方法

常用的正则表达式:

//密码中必须包含大小写 字母、数字、特称字符,长度在8-30之间
密码强:^(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])(?=.*[^a-zA-Z0-9]).{8,30}$;
//密码中必须包含字母(不区分大小写)、数字、特称字符,长度在8-30之间
密码高:^(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[^a-zA-Z0-9]).{8,30}$;
//密码中必须包含字母(不区分大小写)、数字,长度在8-30之间
密码中:^(?=.*[0-9])(?=.*[a-zA-Z]).{8,30}$;
//包含大小写字母和数字的组合,不能使用特殊字符,长度在8-30之间
密码高:^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,30}$;
电子邮箱:^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$;
^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$
整数或者小数:^[0-9]+\.{0,1}[0-9]{0,2}$;
只能输入数字:^[0-9]*$;
信用卡:[0-9]{13,16};
银联卡:^62[0-5]\d{13,16}$;
Visa :^4[0-9]{12}(?:[0-9]{3})?$;
万事达:^5[1-5][0-9]{14}$;
QQ号码:[1-9][0-9]{4,14};
手机号码:^1[3|4|5|7|8][0-9]{9}$ ; //验证手机号码为11位,且以1开头
手机号码:^1[0-9]{10}$ ; //验证手机号码为11位,且以1开头
//身份证号码为15位或者18位,15位时全为数字,18位前17位为数字,最后一位是校验位,可能为数字或
身份证:^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$
7个汉字或14个字符:^[\u4e00-\u9fa5]{1,7}$|^[\dA-Za-z_]{1,14}$

1. html5中的pattern的用法:

//pattern="^1[0-9]{10}$" 
<input type="text" value="" placeholder="请填写您的电话" title="手机号格式不正确" 
pattern="^1[0-9]{10}$" required="">

2. element-ui pattern的用法:

表单中设置 :rules 用来设置验证规则

//rules 
<template>
  <el-form ref="form" :model="user" :rules="rules" label-width="80px">
    <el-form-item label="旧密码" prop="oldPassword">
      <el-input v-model="user.oldPassword" placeholder="请输入旧密码" type="password" show-password />
    </el-form-item>
    <el-form-item label="新密码" prop="newPassword">
      <el-input v-model="user.newPassword" placeholder="请输入新密码" type="password" show-password />
    </el-form-item>
    <el-form-item label="确认密码" prop="confirmPassword">
      <el-input v-model="user.confirmPassword" placeholder="请确认密码" type="password" show-password />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="mini" @click="submit">保存</el-button>
      <el-button type="danger" size="mini" @click="close">关闭</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { updateUserPwd } from "@/api/system/user";
export default {
  data () {
    const equalToPassword = (rule, value, callback) => {
      if (this.user.newPassword !== value) {
        callback(new Error("两次输入的密码不一致"));
      } else {
        callback();
      }
    };
    return {
      test: "1test",
      user: {
        oldPassword: undefined,
        newPassword: undefined,
        confirmPassword: undefined
      },
      // 表单校验
      rules: {
        oldPassword: [
          { required: true, message: "旧密码不能为空", trigger: "blur" }
        ],
        newPassword: [
          { required: true, message: "请输入必须包含大小写字母、数字、特殊符号,至少8个字符的密码", trigger: "blur" },
          { min: 8, max: 20, message: "密码中必须包含大小写字母、数字、特称字符,至少8个字符", trigger: "blur" },
          { pattern: /^(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])(?=.*[^a-zA-Z0-9]).{8,30}$/, message: '密码中必须包含大小写字母、数字、特殊符号,至少8个字符', trigger: 'blur' },
        ],
        confirmPassword: [
          { required: true, message: "确认密码不能为空", trigger: "blur" },
          { required: true, validator: equalToPassword, trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    submit () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          updateUserPwd(this.user.oldPassword, this.user.newPassword).then(
            response => {
              this.msgSuccess("修改成功");
            }
          );
        }
      });
    },
    close () {
      this.$store.dispatch("tagsView/delView", this.$route);
      this.$router.push({ path: "/index" });
    }
  }
};
</script>
上一篇:青岛三日游


下一篇:5、极大似然估计