// 检查是否为手机号
function isPhone(t) {
return null != t.match(/^1[0-9]{10}$/);
}
// 检查是否为邮箱
function isEmail(t) {
var n = /^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@([a-z0-9_][-a-z0-9_]*(\.[-a-z0-9_]+)*\.(aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|mobi|[a-z][a-z])|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?$/i;
return null != t.match(n);
}
// 检查是否为身份证
function isIdcard(t) {
var n = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/;
return n.test(t)
}
// 检查是否为中文名字
function isName(t) {
var n = /^[\u4E00-\u9FA5]{2,4}$/;
return n.test(t)
}
// 检查是否为纯数字
function isNum(t) {
return (/^[0-9]+$/.test(t))
}
// 去除中文
function removeChinese(t) {
var n = /\([^\)]*\)/g;
return t.replace(n, "");
}
// 去除所有空格
function removeAllBlank(t) {
return t.replace(/\s*/g, "");
}
//数据为空检测
function isEmpty(t) {
return void 0 === t || null === t || "" === t || "" === t.trim();
}
//数组存在某值检测
function inArray(t, n) {
for (var e = 0; e < n.length; e++)
if (t == n[e]) return !0;
return !1;
}
// 将数组均分
function splitArray(arr, len) {
let arr_length = arr.length;
let newArr = [];
for (let i = 0; i < arr_length; i += len) {
newArr.push(arr.slice(i, i + len));
}
return newArr;
}
// Toast提示
function showToast(title, icon, duration) {
wx.showToast({
title: title || '网络开小差了...',
icon: icon || 'none',
duration: duration || 1000
})
}
//新页面跳转
function jumpPage(url, isRedirect) {
if (isRedirect) {
wx.redirectTo({
url: url
})
} else {
wx.navigateTo({
url: url
});
}
}
//加减乘除封装
function counter(a, b, c) {
switch (c) {
case 'add':
var r1, r2, m;
try {
r1 = a.toString().split(".")[1].length;
} catch (e) {
r1 = 0;
}
try {
r2 = b.toString().split(".")[1].length;
} catch (e) {
r2 = 0;
}
m = Math.pow(10, Math.max(r1, r2));
return (a * m + b * m) / m;
break;
case 'sub':
var r1, r2, m, n;
try {
r1 = a.toString().split(".")[1].length;
} catch (e) {
r1 = 0;
}
try {
r2 = b.toString().split(".")[1].length;
} catch (e) {
r2 = 0;
}
m = Math.pow(10, Math.max(r1, r2));
n = (r1 >= r2) ? r1 : r2;
return ((a * m - b * m) / m).toFixed(n);
break;
case 'mul':
var m = 0,
s1 = a.toString(),
s2 = b.toString();
try {
m += s1.split(".")[1].length;
} catch (e) {}
try {
m += s2.split(".")[1].length;
} catch (e) {}
return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
break;
case 'div':
var t1 = 0,
t2 = 0,
r1, r2;
try {
t1 = a.toString().split(".")[1].length;
} catch (e) {}
try {
t2 = b.toString().split(".")[1].length;
} catch (e) {}
r1 = Number(a.toString().replace(".", ""));
r2 = Number(b.toString().replace(".", ""));
return (r1 / r2) * Math.pow(10, t2 - t1);
break;
}
}
module.exports = {
isPhone: isPhone,
isEmail: isEmail,
isIdcard:isIdcard,
isName: isName,
isNum: isNum,
removeChinese: removeChinese,
removeAllBlank: removeAllBlank,
isEmpty: isEmpty,
inArray: inArray,
splitArray: splitArray,
showToast: showToast,
jumpPage: jumpPage,
counter: counter,
}
阅读(1018) | 评论(0) | 转发(0) |