vue 当中的表单验证

安排的任务中需要对表单的元素进行验证,用的正则判断

  1. IP地址
    正则表达式/^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$/

  2. 端口号
    正则表达式/^([0-9]|[1-9]\d{1,3}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])$/

在对应的item中绑定rules、

<el-form ref="form" :model="form" :rules="rules" label-width="80px">
data(){
  return{
    // 表单校验
      rules: {
        dsname: [
          { required: true, message: "数据源名称不能为空", trigger: "blur" }
        ],
        dstype: [
          { required: true, message: "数据源类型:1-mysql,2-oracle,3-mqtt不能为空", trigger: "change" }
        ],
        dsip: [
          { required: true, message: "数据源ip地址不能为空", trigger: "blur" },
          { pattern: /^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$/, message: '请输入正确的ip地址', trigger: ['blur', 'change']}
        ],
        dsport: [
          { required: true, message: "数据源端口不能为空", trigger: "blur" },
          { pattern: /^([0-9]|[1-9]\d{1,3}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])$/, message: '请输入正确的端口', trigger: ['blur', 'change']}
        ],
        dname: [
          { required: true, message: "数据库名或主题不能为空", trigger: "blur" },
          { pattern: /^[a-z][a-z0-9_]*$/ , message: '数据库名以小写英文字母开头,且只能包含英文字母、数字、下划线', trigger: ['blur', 'change']}
        ],
        dsuser: [
          { required: true, message: "数据源用户名不能为空", trigger: "blur" },
          { pattern: /^[^\u4e00-\u9fa5]{0,}$/ , message: '数据库名不能有中文', trigger: ['blur', 'change']}
        ],
        dspassword: [
          { required: true, message: "数据源密码不能为空", trigger: "blur" },
          { pattern: /^(\w){0,100}$/ , message: '密码长度最长100位', trigger: ['blur', 'change']}
        ]
      }
  }
}
posted @ 2021-04-07 15:45  张三丰学Java  阅读(378)  评论(0编辑  收藏  举报