正则校验总结(邮箱、电话号码、身份证、ip地址等等)

10 篇文章 0 订阅
 /**
   * 输入字符串非空校验 长度校验
   * @param inputstr
   * @param len
   */
  static inputStrlenCheck(inputstr: string, len: number): string {
    let content = null;
    if (!inputstr || inputstr.trim() === '' || inputstr === null || inputstr === undefined) {
      content = '为必填项';
    } else {
      inputstr = inputstr.trim();
      let tesTmp=deepCopy(inputstr);
      tesTmp=tesTmp.replace(/[\u4e00-\u9fa5]/g,'aa');
      if (tesTmp.length > len) {
        content = '值长度不能超过' + len + '个字符(一个汉字占2个字符)!';
      }
    }
    return content;

  }

  /**
   * 选择列表非空校验
   * @param inputstr
   */
  static inputlistCheck(inputlist: any): string {
    let content = null;
    if (inputlist === null || inputlist === undefined || inputlist.length === 0) {
      content = '为必填项';
    }
    return content;

  }

  /**
   *邮箱格式校验
   * @param email
   */
  static checkEmail(email: string) {
    let content = null;
    const regex = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
    //判断
    if (!(/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/.test(email))) {
      content = '邮箱格式不正确';
    }
    return content;
  }

  /**
   * 电话号码校验
   * @param number
   */
  static checkPhoneNumber(number) {
    let content = null;
    if (number.indexOf('-') >= 0) {
      if (!(/^(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,8}$/.test(number))) { //(/^(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,11}$/
        content = '终端使用者座机号码格式有误,请重填';
      }
    } else if (!(/^1(3|4|5|7|8|9)\d{9}$/.test(number))) {
        content = '终端使用者手机号码格式有误,请重填';
      }
      return content;
    }
  /**
   * 手机号码校验
   * @param number
   */
  static  checkMobileNumber(number){
      let content = null;
     if (!(/^1(3|4|5|7|8|9)\d{9}$/.test(number))) {
        content = '终端使用者手机号码格式有误,请重填';
      }
      return content;
    }

  /**
   * 身份证号码校验
   * @param idNumber
   */
  static checkIDNumber(code){
    const city={11:'北京',12:'天津',13:'河北',14:'山西',15:'内蒙古',21:'辽宁',22:'吉林',23:'黑龙江 ',31:'上海',32:'江苏',33:'浙江',34:'安徽',35:'福建',36:'江西',37:'山东',41:'河南',42:'湖北 ',43:'湖南',44:'广东',45:'广西',46:'海南',50:'重庆',51:'四川',52:'贵州',53:'云南',54:'西藏 ',61:'陕西',62:'甘肃',63:'青海',64:'宁夏',65:'新疆',71:'台湾',81:'香港',82:'澳门',91:'国外 '};
    let tip = null;
    let pass= true;

    if(!code || !/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/i.test(code)){
      tip = '身份证号格式错误';
      pass = false;
    }

    else if(!city[code.substr(0,2)]){
      tip = '地址编码错误';
      pass = false;
    }

    return tip;
    }
  /**
   * 检测两个ip顺序是否正确,前面的perIp小于后面的nextIp
   * @param perIp
   * @param nextIp
   */
  static checkIPOrder(perIp: string, nextIp: string) {
    let content = null;
    const temp1 = perIp.split('.');
    const temp2 = nextIp.split('.');
    for (let i = 0; i < temp1.length; i++) {
      const per = parseInt(temp1[i].trim(), 10);
      const nex = parseInt(temp2[i].trim(), 10)
      if (per > nex) {
        content = '属性值前者要小于后者';
        return content;
      } else if (per < nex) {
        return content;
      }
    }

    return content;
  }

  /**
   * 检测两个ipv6顺序是否正确,前面的perIp小于后面的nextIp
   * @param perIp
   * @param nextIp
   */
  static checkIPv6Order(perIp: string, nextIp: string) {
    let content = null;
    const temp1 = perIp.split(':');
    const temp2 = nextIp.split(':');
    for (let i = 0; i < temp1.length; i++) {
      const per = parseInt(temp1[i].trim(), 10);
      const nex = parseInt(temp2[i].trim(), 10)
      if (per > nex) {
        content = '属性值前者要小于后者';
        return content;
      } else if (per < nex) {
        return content;
      }

    }

    return content;
  }

  /**
   * 检测两个mac顺序是否正确,前面的perMac小于后面的nextMac
   * @param perMac
   * @param nextMac
   */
  static checkMacOrder(perMac: string, nextMac: string) {
    let content = null;
    const temp1 = perMac.split('-');
    const temp2 = nextMac.split('-');
    for (let i = 0; i < 4; i++) {
      const per = parseInt(temp1[i].trim(), 16);
      const nex = parseInt(temp2[i].trim(), 16)
      if (per > nex) {
        content = '属性值前者要小于后者';
        return content;
      } else if (per < nex) {
        return content;
      }
    }
    return content;
  }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值