各種程式碼段(驗證、方法)
阿新 • • 發佈:2020-12-25
技術標籤:jqueryjavascript
/** * 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;
}