各种实用的js功能
//判断是不是空格
const isNull = (str) =>{
if ( str == "" ) {
return true
}
var regu = "^[ ]+$";
var re = new RegExp(regu);
if(re.test(str)){
return true
}else{
return false
}
}
/**
* 手机号验证
* */
const verifyPhone = (e) =>{
let myreg = /^1[3-9][0-9]{9}$/;
if (!myreg.test(e)) {
console.log('请输入正确的手机号')
return false;
}else{
return true;
}
}
/**
* 最多保留两位小数点
* */
const unifyNumber = (num)=> {
if (num === '') {
return 0
} else {
let handleNum = parseFloat(num)
let isToFixed = handleNum.toString().includes('.') && handleNum.toString().split('.')[1].length > 2
if (isToFixed) {
return handleNum.toFixed(2)
} else {
return handleNum
}
}
}
/**
* 手机号中间隐藏四位数
* */
const geTel = (tel) =>{
var reg = /^(\d{3})\d{4}(\d{4})$/;
return tel.replace(reg, "$1****$2");
}
/**
* 将2020-02-02转化成2020年02月02日
* */
const converTime = (e)=>{
// 将日期格式转化
var reg =/(\d{4})\-(\d{2})\-(\d{2})/;
var date = e.replace(reg,"$1年$2月$3日");
return date;
}
//将时间戳转为日期
const dealTime = (e) =>{
if(!e)return'';
let dd = e+'';
let date;
if(dd.length == 10){
let timestamp = Number(dd)
date = new Date(parseInt(timestamp) * 1000);
}else{
let timestamp = Number(dd)
date = new Date(parseInt(timestamp));
}
let Year = date.getFullYear();
let Moth = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
let Day = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate());
let Hour = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours());
let Minute = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes());
let Sechond = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
let GMT = Year + '-' + Moth + '-' + Day + ' '+ Hour +':'+ Minute + ':' + Sechond;
return GMT
},
/*
*获取文件后缀
*/
const lastfile = (url) =>{
let file_name = url.match(/\.([^\.]+)$/)[1];//获取文件后缀
return file_name
}
/**
*删除文件后缀
*/
const deleteFileLast = (url) =>{
let aa = url.substring(0, url.lastIndexOf("."));//删除文件路径后缀
return aa
}