常用js方法(日期及手机,数字,邮箱等验证)

/**
 * 获取当前日期:x年x月x日
 */
export const getNowDate = () => {
  let time = new Date();
  let year = time.getFullYear();
  let month = time.getMonth() + 1 + "";
  let data = time.getDate() + "";
  if (month.length < 2) {
    month = "0" + month;
  }
  if (data.length < 2) {
    data = "0" + data;
  }
  let newtime = year + "年" + month + "月" + data + "日";
  return newtime;
};
//时间戳转化为时间
  timestampToTime:function(timestamp) {
    var date = new Date(timestamp);//时间戳为10位需*1000,时间戳为13位的话不需乘1000
    var Y = date.getFullYear() + '-';
    var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
    var D = (date.getDate()<10?'0'+date.getDate():date.getDate()) + ' ';
    var h = (date.getHours()<10?'0'+date.getHours():date.getHours()) + ':';
    var m = (date.getMinutes()<10?'0'+date.getMinutes():date.getMinutes()) + ':';
    var s = (date.getSeconds()<10?'0'+date.getSeconds():date.getSeconds());
    return Y+M+D+h+m+s;
  },
/**
 * 获取当前月份的第一天
 */
 export const getDateFirstDay = (val) => {
  var date = new Date(val);
  date.setDate(1);
  var month = parseInt(date.getMonth()+1);
  var day = date.getDate();
  var time = '00:00:00'
  if (month < 10) {
  month = '0' + month
  }
  if (day < 10) {
  day = '0' + day
  }
  console.log(date.getFullYear() + '-' + month + '-' + day + ' ' + time);
  return date.getFullYear() + '-' + month + '-' + day + ' ' + time
};
/**
 * 获取当前月份的最后一天
 */
 export const getDateLastDay = (val) => {
  var date=new Date(val);
  var currentMonth=date.getMonth();
  var nextMonth=++currentMonth;
  var nextMonthFirstDay=new Date(date.getFullYear(),nextMonth,1);
  var oneDay=1000*60*60*24;
  var lastTime = new Date(nextMonthFirstDay-oneDay);
  var month = parseInt(lastTime.getMonth()+1);
  var day = lastTime.getDate();
  var time = '23:59:59'
  if (month < 10) {
  month = '0' + month
  }
  if (day < 10) {
  day = '0' + day
  }
  console.log(typeof(date.getFullYear() + '-' + month + '-' + day + ' ' + time));
  return date.getFullYear() + '-' + month + '-' + day + ' ' + time
};
/**
 * 邮政编码校验
 * 返回值: true/false
 */
export const postCodeCheck = value => {
  let reg = /^[0-9]{6}$/;
  let result = reg.test(value);
  return result;
};
/**
 * 手机号码校验
 * 返回值: true/false
 */
export const mobileCheck = value => {
  let reg = /^1[0-9][0-9]{9}$/;
  let result = reg.test(value);
  return result;
};

/**
 * 电话号码校验
 * 返回值: true/false
 */
export const telephoneCheck = value => {
  let reg = /^0\d{2,3}-?\d{7,8}$/;
  let result = reg.test(value);
  return result;
};

/**
 * 网址校验
 * 返回值: true/false
 */
export const urlCheck = value => {
  let str = `^((https|http|ftp|rtsp|mms)?://)`
        + `?(([0-9a-z_!~*'().&=+$%-]+:)?[0-9a-z_!~*'().&=+$%-]+@)?`// ftp的user@
        + `?(([0-9]{1,3}\.){3}[0-9]{1,3}`// IP形式的url
        + `|`// 允许IP和DOMAIN
        + `([0-9a-z_!~*'()-]+\.)*`// 域名-www
        + `([0-9a-z][0-9a-z]{0,61})?[0-9a-z]\.`// 二级域名
        + `[a-z]{2,6})`// first level domain-.com or .museum
        + `(:[0-9]{1,4})?`// 端口-:80
        + `((/?)|`// a slash isn't required if there is na file name
        + `(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$`;
  let reg = new RegExp(str);
  let result = reg.test(value);
  return result;
};

/**
 * 传真号码校验
 * 返回值: true/false
 */
export const faxCheck = value => {
  let reg = /^(\d{3,4})?[-]?\d{7,8}$/;
  let result = reg.test(value);
  return result;
};

/**
 * 邮箱校验
 * 返回值: true/false
 */
export const emailCheck = value => {
  let reg = /^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/;
  let result = reg.test(value);
  return result;
};
// 卡号添加掩码
export const acctNewHidden = (value) => {
  let txt = '';
  txt = value.substring(0, 4) + ' **** **** ' + value.substring(value.length - 4, value.length);
  // txt = '**** **** **** ' + value.substring(value.length - 4, value.length);
  return txt;
};
/**
 * 身份证日期格式校验 1992.12.07(1992-12-07、1992/12/07、19921207) 或 '长期'
 * 返回值: true/false
 */
export const checkDate = (value, word = false) => {
  let reg = /^(\d{4})((\.((0[1-9])|(1[0-2]))\.)|(\-((0[1-9])|(1[0-2]))\-)|(\/((0[1-9])|(1[0-2]))\/)|((0[1-9])|(1[0-2])))((0[1-9])|([1-2][0-9])|(3[0-1]))$/;
  let result = reg.test(value);
  if (!result && word) {
      result = value.trim() == '长期';
    }
  return result;
};

/**
 * 姓名中间不能有空格
 * 返回值: true/false
 */
export const checkName = value => {
  var reg = /^[\u4E00-\u9FA5]{1,6}$/;
  let result = reg.test(value);
  return result;
};

/**
 * 检验纯数字
 * 返回值: true/false
 */
export const checkNumber = value => {
  var reg = /^[0-9]*$/;
  let result = reg.test(value);
  return result;
};
/**
 * 检验纯数字(包含小数点)
 * 返回值: true/false
 */
export const checkDotNumber = value => {
  var reg = /([1-9]\d*\.?\d*)|(0\.\d*[1-9])/;
  let result = reg.test(value);
  return result;
};
/**
 * 检验纯汉字
 * 返回值: true/false
 */
export const checkChinese = value => {
  var reg = /.*[\u4e00-\u9fa5]+.*$/;
  let result = reg.test(value);
  return result;
};

/**
 * 检验英文和数字
 * 返回值: true/false
 */
export const checkEnAndNum = value => {
  var reg = /^[A-Za-z0-9]+$/;
  let result = reg.test(value || null);
  return result;
};
/**
 * 搜索高亮
 * 返回值: 
 */
 export const searchKey = (value, key) => {
  let replaceReg = new RegExp(key, 'ig')
  let replaceString = `<span style="color: #16B4CF">${key}</span>`
  value = value.replace(replaceReg, replaceString)
  return value
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值