vue封装 element-ui form表单验证 正则匹配手机号 自定义校验表格内容

在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">
    "姓名" :label-width="formLabelWidth" prop="newName">
      "formedit.newName" autocomplete="off">
    
    "手机号" :label-width="formLabelWidth" >
      "formedit.newPhone" autocomplete="off" disabled>
    
    "权限" :label-width="formLabelWidth" prop="newRoleId">
      "formedit.newRoleId" placeholder="请选择权限">
        "超级管理员" :value="1">
        "管理员" :value="2">
      
    
  

需要验证手机写的是 element框架中prop="phone"验证规则


	"手机号" prop="phone">
      "formAdd.phone" autocomplete="off">
    

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" }]
		      },
		 }

你可能感兴趣的:(vue,正则表达式)