各种代码段(验证、方法)

/**
 * form表单序列化转换为Json对象
 *用法:$("form").serializeJson()      //得到一个Json对象
 */
$.fn.serializeJson = function() {
	var serializeObj = {};
	var array = this.serializeArray();
	var str = this.serialize();
	var flag = true;
	$(array).each(function() {
		if (serializeObj[this.name]) {
			if ($.isArray(serializeObj[this.name])) {
				if (this.value == "on") {
					serializeObj[this.name].push("true");
				} else {
					serializeObj[this.name].push(this.value);
				}
			} else {
				if (this.value == "on") {
					serializeObj[this.name] = [serializeObj[this.name], "true"];
				} else {
					serializeObj[this.name] = [serializeObj[this.name], this.value];
				}
			}
		} else {
			if (this.value == "on") {
				serializeObj[this.name] = "true";
			} else {
				serializeObj[this.name] = this.value;
			}
		}
	});
	return serializeObj;
};
//获取url路径后面的参数
function getQueryString(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
	var r = window.location.search.substr(1).match(reg);
	if (r != null) return unescape(r[2]);
	return null;
}
//iframe获取顶层url参数
function getUrlvalue(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
	var r = top.window.location.search.substr(1).match(reg); //匹配目标参数
	if (r != null) return r[2];
	return ""; //返回参数值
}
//('2020-03-18T18:55:27.207', '{y}-{m}-{d} | {h}:{i}' )
function utc_time(time, cFormat) { 
	if (!time) {
		return '';
	}
	var myDate = new Date(time + '+0800');
	if (myDate == 'Invalid Date') {
		time = time.replace(/T/g, ' ');
		time = time.replace(/-/g, '/');
		time = time.replace(/\.\d+/, ' '); //去掉毫秒
		myDate = new Date(time + '+0800');
	}
	time = myDate;


	if (time == null) {
		return '';
	}
	if (arguments.length === 0) {
		return null
	}
	var format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
	var date
	if (typeof time === 'object') {
		date = time
	} else {
		if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
			time = parseInt(time)
		}
		if ((typeof time === 'number') && (time.toString().length === 10)) {
			time = time * 1000
		}
		date = new Date(time)
	}
	var formatObj = {
		y: date.getFullYear(),
		m: date.getMonth() + 1,
		d: date.getDate(),
		h: date.getHours(),
		i: date.getMinutes(),
		s: date.getSeconds(),
		a: date.getDay()
	}
	var time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
		var value = formatObj[key]
		// Note: getDay() returns 0 on Sunday
		if (key === 'a') {
			return ['日', '一', '二', '三', '四', '五', '六'][value]
		}
		//return value.toString().padStart(2, '0')  //时间补零,padStart可能出现兼容问题
		return value.toString()
	})
	return time_str
}
//2020-02-29T20:00:00.000Z
function time_Z(s) {
	var d = new Date(s);
	var h = d.getHours(); //12
	var m = d.getMinutes(); //12
	if (h < 10) {
		h = "0" + h;
	}
	if (m < 10) {
		m = "0" + m;
	}
	// console.log(h + ':' + m);
	return h + ':' + m;
}
// 时间戳转时间
// sjc(1583128615000)
function sjc(timestamp) {
	var date = new Date(timestamp); //时间戳为10位需*1000,时间戳为13位的话不需乘1000
	Y = date.getFullYear() + '-';
	M = (date.getMonth() + 1 <= 9 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
	D = (date.getDate() <= 9 ? '0' + date.getDate() : date.getDate()) + ' ';
	h = (date.getHours() <= 9 ? '0' + (date.getHours()) : date.getHours()) + ':';
	m = (date.getMinutes() <= 9 ? '0' + (date.getMinutes()) : date.getMinutes()) + ':';
	s = (date.getSeconds() <= 9 ? '0' + (date.getSeconds()) : date.getSeconds());
	return Y + M + D + h + m + s;
}
// 时间转时间戳
// time("2014/4/23 18:55:49")
function time(t) {
	var date = new Date(t);
	return date.getTime();
}
// 时间转换
function timeForMat(count) {
	// 拼接时间
	const time1 = new Date()
	const time2 = new Date()
	if (count === 1) {
		time1.setTime(time1.getTime() - (24 * 60 * 60 * 1000))
	} else {
		if (count >= 0) {
			time1.setTime(time1.getTime())
		} else {
			if (count === -2) {
				time1.setTime(time1.getTime() + (24 * 60 * 60 * 1000) * 2)
			} else {
				time1.setTime(time1.getTime() + (24 * 60 * 60 * 1000))
			}
		}
	}

	const Y1 = time1.getFullYear()
	const M1 = ((time1.getMonth() + 1) > 9 ? (time1.getMonth() + 1) : '0' + (time1.getMonth() + 1))
	const D1 = (time1.getDate() > 9 ? time1.getDate() : '0' + time1.getDate())
	const timer1 = Y1 + '-' + M1 + '-' + D1 // 当前时间

	time2.setTime(time2.getTime() - (24 * 60 * 60 * 1000 * count))
	const Y2 = time2.getFullYear()
	const M2 = ((time2.getMonth() + 1) > 9 ? (time2.getMonth() + 1) : '0' + (time2.getMonth() + 1))
	const D2 = (time2.getDate() > 9 ? time2.getDate() : '0' + time2.getDate())
	const timer2 = Y1 + '-' + M2 + '-' + D2 // 之前的7天或者30天
	return [timer2, timer1]
}
// 获取最近30天
function thirtyDays() {
	const timer = timeForMat(30)
	return timer[0]
	return timer[1]
}

// 获取最近7天
function sevenDays() {
	const timer = this.timeForMat(7)
	return timer[0]
	return timer[1]
}
//获取当天
function today() {
	const timer = this.timeForMat(0)
	return timer[0]
}
// 获取当前时间
function newday() {
	var date = new Date();
	var year = date.getFullYear();
	var month = date.getMonth() + 1;
	var day = date.getDate();
	if (month < 10) {
		month = "0" + month;
	}
	if (day < 10) {
		day = "0" + day;
	}
	return year + "-" + month + "-" + day;
}
// 手机号格式验证
function isPhoneNo(phone) {
	var pattern = /^1[345678]\d{9}$/;
	if (!pattern.test(phone)) {
		layer.msg("手机号有误");
		return false
	}
	return true
}
// 身份证号码/驾驶证号验证
function isID_num(num) {
	var idCardReg = /^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$/;
	if (!idCardReg.test(num)) {
		layer.msg("身份证号码格式错误");
		return false;
	}
}
// 车牌号验证
function isCarNo(num) {
	var re =
		/^(([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z](([0-9]{5}[DF])|([DF]([A-HJ-NP-Z0-9])[0-9]{4})))|([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z][A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳使领]))$/;
	if (!licensePlate.test(num)) {
		layer.msg("车牌号格式错误");
		return false;
	}
}
//金额格式转换
// s 金额
// n 保留几位小数
function fmoney(s, n) {
	n = n > 0 && n <= 20 ? n : 2;
	s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";
	var l = s.split(".")[0].split("").reverse(),
		r = s.split(".")[1];
	t = "";
	for (i = 0; i < l.length; i++) {
		t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");
	}
	return t.split("").reverse().join("") + "." + r;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值