在methods:中写:
//检查手机号 isCellPhone(val) { if (!/^1(3|4|5|6|7|8)\d{9}$/.test(val)) { return false; } else { return true; } },在template中
<el-form :model="formedit" ref="ruleForms" :rules="ruleEdit" label-position="left"> <el-form-item label="姓名" :label-width="formLabelWidth" prop="newName"> <el-input v-model="formedit.newName" autocomplete="off"></el-input> </el-form-item> <el-form-item label="手机号" :label-width="formLabelWidth" > <el-input v-model="formedit.newPhone" autocomplete="off" disabled></el-input> </el-form-item> <el-form-item label="权限" :label-width="formLabelWidth" prop="newRoleId"> <el-select v-model="formedit.newRoleId" placeholder="请选择权限"> <el-option label="超级管理员" :value="1"></el-option> <el-option label="管理员" :value="2"></el-option> </el-select> </el-form-item> </el-form>需要验证手机写的是 element框架中prop="phone"验证规则
<el-form-item label="手机号" prop="phone"> <el-input v-model="formAdd.phone" autocomplete="off"></el-input> </el-form-item>data中
data() { var checkphone = (rule, value, callback) => { // let phoneReg = /(^1[3|4|5|6|7|8|9]\d{9}$)|(^09\d{8}$)/; if (value == "") { callback(new Error("请输入手机号")); } else if (!this.isCellPhone(value)) {//引入methods中封装的检查手机格式的方法 callback(new Error("请输入正确的手机号!")); } else { callback(); } }; return { rules: { userName: [{ required: true, message: "请输入姓名", trigger: "blur" }], phone: [{ required: true, validator: checkphone, trigger: "blur" }],//validator: checkphone是验证手机的引入checkphone函数 roleId: [{ required: true, message: "请选择权限", trigger: "change" }] }, }