例子: https://gitee.com/akff/ak-mini-program
實(shí)現(xiàn)效果
實(shí)現(xiàn)代碼
if (isTool
.isHaveValue(value, "value不能為空")
.isNumber(number, "number不能為空", "number不是數(shù)字類型")
.isEmail(email, "email不能為空", "email不是郵箱格式")
.isPhone(phone, "phone不能為空", "phone不是正確電話格式")
.isPrice(price, "price不能為空", "price不是正確價(jià)格格式")
.isResult === true) {
// 通過(guò)校驗(yàn)
} else {
// 不通過(guò)校驗(yàn)
}
核心代碼
const regExpression = require('./AKRegExpression.js')
// 方法: 是否數(shù)字
function checkNumber(str, isHaveZero) {
let regE = new RegExp('[0-9]', 'g')
//是否已經(jīng)有 點(diǎn)'.'
let isHavePoint = false
for (let index in str) {
if (str[index] == '.') { //是否為'.'
// 判斷是否有重復(fù)逗號(hào)
if (isHavePoint) {
return false
}
isHavePoint = true
} else if (regE.exec(str) == undefined) { //是否為數(shù)字
//是否為'.'特殊符號(hào)
return false
} else if (isHaveZero != true && index == 0 && str[0] == "0") {
//是否第一位為0
return false
}
}
//循環(huán)沒(méi)有跳出,則返回true
return true
}
// 結(jié)果(只要不是進(jìn)入bundleFalse方法,就是true)
const isResult = true
// 判斷方法
const tool = {
//判斷開(kāi)始時(shí)間和結(jié)束時(shí)間范圍
isEnableTimeRange(startTime, endTime, splitSymbol, warnNote) {
let start = startTime.split(splitSymbol).join("")
let end = endTime.split(splitSymbol).join("")
let note = ""
if (Number(start) > Number(end)) { //年 開(kāi)始日期 在結(jié)束日期前
note = (warnNote || "開(kāi)始時(shí)間不能大于結(jié)束時(shí)間")
}
if (note != "") {
wx.showToast({
title: note,
icon: 'none'
})
return bundleFalse
}
return this
},
//非空判斷(數(shù)組/對(duì)象/字符串)
isHaveValue(data, note) {
if (data === null) {
data = undefined
}
let typeStr = typeof data
let isHaveValue = true
switch (typeStr) {
case 'undefined': //空
isHaveValue = false
break
case 'string': //字符串
data == "" && (isHaveValue = false)
break
case 'object': //數(shù)組或?qū)ο? Object.keys(data).length ||
(isHaveValue = false)
default:
break
}
if (note && !isHaveValue) {
wx.showToast({
title: note || "",
icon: 'none',
})
}
return isHaveValue ? this : bundleFalse
},
//判斷是否電話
isPhone(data, nullNote = '手機(jī)號(hào)不能為空', notPhoneNote = '手機(jī)號(hào)格式不正確') {
let isHave = this.isHaveValue.apply(this, [data, nullNote]).isResult
let isPhoneNum = false
if (isHave) {
isPhoneNum = regExpression.phoneRegExp.test(data)
isPhoneNum || wx.showToast({
title: notPhoneNote,
icon: 'none'
})
}
return isPhoneNum ? this : bundleFalse
},
//判斷是否數(shù)字
isNumber(data, nullNote, notNumbernote, isHaveZero) {
let isNumber = true
let note = ""
//判斷是否為0 (解決 0 == "")
let checkZero = data + ""
if (checkZero == "") {
isNumber = false
note = nullNote
} else if (!checkNumber(checkZero, isHaveZero)) {
isNumber = false
note = notNumbernote
}
isNumber == false && wx.showToast({
title: note,
icon: 'none'
})
return isNumber ? this : bundleFalse
},
// 是否價(jià)格
isPrice(data, nullNote, notNumbernote, isCanNull) {
// 判斷是否為null
if (isCanNull && !data) return this
//正則
var reg = regExpression.priceExp
// 轉(zhuǎn)為字符串
let checkZero = data + ""
let note = checkZero == "" ? nullNote : notNumbernote
const isPrice = reg.test(checkZero)
if (!isPrice) {
wx.showToast({
title: note,
icon: 'none'
})
}
return isPrice ? this : bundleFalse
},
// 檢查是否是郵箱
isEmail(value, nullNote, notPhoneNote) {
if (!this.isHaveValue.apply(this, [value, nullNote]).isResult) {
return bundleFalse
}
let expr = /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/
if (expr.test(value)) {
return this
} else {
notPhoneNote && wx.showToast({
title: notPhoneNote,
icon: 'none'
})
return bundleFalse
}
},
// 角標(biāo)是否選中
isPickerIndex(data, note) {
let isHaveValue = data != null && data != -1
if (note && !isHaveValue) {
wx.showToast({
title: note || "",
icon: 'none',
})
}
return isHaveValue ? this : bundleFalse
},
}
// 創(chuàng)建過(guò)渡對(duì)象
let bundleFalse = {
isResult: false
}
// 插入當(dāng)校驗(yàn)false時(shí)的回調(diào)-空方法
const funcNames = Object.keys(tool)
for (let funcName of funcNames) {
if (typeof tool[funcName] == "function") {
bundleFalse[funcName] = function() {
return this
}
}
}
module.exports = {
// 結(jié)果
isResult,
//判斷開(kāi)始時(shí)間和結(jié)束時(shí)間范圍
isEnableTimeRange: tool.isEnableTimeRange,
//是否有值判斷(數(shù)組/對(duì)象/字符串)
isHaveValue: tool.isHaveValue,
//判斷是否電話
isPhone: tool.isPhone,
//判斷是否數(shù)字
isNumber: tool.isNumber,
// 是否價(jià)格
isPrice: tool.isPrice,
// 檢查是否是郵箱
isEmail: tool.isEmail,
// 角標(biāo)是否選中
isPickerIndex: tool.isPickerIndex,
}