电话号码、IP格式、网络端口、Email 格式校验

12 篇文章 0 订阅

电话号码、IP格式、网络端口、Email 格式校验,代码如下:

 <el-form ref="auditForm" :label-position="labelPosition" :model="auditForm" :rules="dataRule" label-width="160px" size="mini">
    <el-row :gutter="24">
       <el-col :span="8">
          <el-form-item label="电话" prop="phone">
             <el-input v-model="auditForm.phone"></el-input>
          </el-form-item>
       </el-col>
    </el-row>
    <el-row :gutter="24">
       <el-col :span="8">
          <el-form-item label="IP" prop="ip">
             <el-input v-model="auditForm.ip"></el-input>
          </el-form-item>
       </el-col>
    </el-row>
    <el-row :gutter="24">
       <el-col :span="8">
          <el-form-item label="端口" prop="port">
             <el-input v-model="auditForm.port"></el-input>
          </el-form-item>
       </el-col>
    </el-row>
    <el-row :gutter="24">
       <el-col :span="8">
          <el-form-item label="Email" prop="Email">
             <el-input v-model="auditForm.Email"></el-input>
          </el-form-item>
       </el-col>
    </el-row>
  </el-form>
            
<script>
export default {
  data() {
  //电话号码格式校验
    var checkPhone = (rule, value, callback) => {
      const phoneReg = /^1[3|4|5|7|8|9][0-9]{9}$/;
      if (!value) {
        return callback(new Error("负责人电话不能为空"));
      }
      setTimeout(() => {
        if (phoneReg.test(value)) {
          callback();
        } else {
          callback(new Error("负责人电话格式不正确"));
        }
      }, 100);
    };
    // ip验证
    var checkIp = (rule, value, callback) => {
      var reg =
        /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
      if (!value) {
        return callback(new Error("IP地址不能为空"));
      }
      if (!reg.test(value)) {
        return callback(new Error("请输入正确的IP地址"));
      } else {
        return callback();
      }
    };
    //端口校验
    var checkPort = (rule, value, callback) => {
      var reg =
        /^([0-9]|[1-9]\d{1,3}|[1-5]\d{4}|6[0-4]\d{4}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])$/;
      if (!value) {
        return callback(new Error("端口不能为空"));
      }
      if (value != null && !reg.test(value)) {
        return callback(new Error("请输入正确的端口"));
      } else {
        return callback();
      }
    };
    //Email校验
    var checkEmail = (rule, value, callback) => {
      var reg =
         /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/;
      if (!value) {
        return callback(new Error("Emai不能为空"));
      }
      if (value != null && !reg.test(value)) {
        return callback(new Error("请输入正确的Emai"));
      } else {
        return callback();
      }
    };
    return {
      auditForm:{
        phone: "", 
        ip:"",
        port: "", 
        Email:"",
      },
      dataRule:{
        phone: [
          { required: true, validator: checkPhone, trigger: "blur" },
        ], 
        ip:[
          { required: true, validator: checkIp, trigger: "blur" },
        ],,
        port: [
          { required: true, validator: checkPort, trigger: "blur" },
        ],, 
        Email:[
          { required: true, validator: checkEmail, trigger: "blur" },
        ],,
      }
     }
   }
 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值