目录
值类型校验
手机号校验
密码校验
中文校验
邮箱校验
检测设备系统
检测是否是手机设备
检测 ios
检测 android
检测 Mac 系统
检测 Windows 系统
检测 Safari 浏览器
检测 flash 插件
检测数据类型
检测 Null
检测 Undefined
检测 Boolean
检测 String
检测 Number
检测 NaN
检测 Object
检测 Array
检测 RegExp
检测 Map
检测 Promise
检测 Date
检测 Math
检测 JSON
其他
HTML结构中移除html标签
生成UUID
时间处理器
/**
* 手机号校验
* @param {String | Number} value
* @returns {Boolean}
*/
export function isMobile(value) {
const REG_MOBILE = /^1(3|4|5|6|7|8|9)[0-9]{9}$/
return REG_MOBILE.test(value)
}
/**
* 密码校验
* @param {String | Number} value
* @returns {Boolean}
*/
export function(value) {
const REG_PASSWORD = /^[0-9a-zA-Z]{6,20}$/
return REG_PASSWORD.test(value)
}
/**
* 中文字符校验
* @param {String} value
* @returns {Boolean}
*/
export function isChineseString(value) {
const REG_CHINESE_STRING = /[^(\u4e00-\u9fa5)]{8,24}/
return REG_CHINESE_STRING.test(value)
}
/**
* 邮箱验证
* @param {String} value
* @returns {Boolean}
*/
export function isEmail(value) {
const REG_EMAIL = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/
return REG_EMAIL.test(value)
}
/**
* 是否是手机设备
*/
export function isMobileDevice() {
const userAgent = window.navigator.userAgent
return !!userAgent.match(/AppleWebKit.*Mobile.*/)
}
/**
* 判断是否是IOS系统
*/
export function isIOS() {
const REG_IOS = /iphone|ipad|ipod/
return REG_IOS.test(userAgent)
}
/**
* 判断是否是 android 系统
*/
export function isAndroid() {
const REG_ANFROID = /android/
return REG_ANFROID.test(userAgent)
}
/**
* 判断是否是 MAC 系统
*/
export function isMac() {
const REG_MAC = /macintosh|mac os x/i
return REG_MAC.test(userAgent)
}
/**
* 判断是否是 WINDOWS 系统
*/
export function isWindows() {
const REG_WINDOWS = /windows|win32/i
return REG_WINDOWS.test(userAgent)
}
/**
* 判断是否是 Safari 浏览器
*/
export function isSafari() {
const REG_SAFARI = /safari/
const REG_CHROME = /chrome/
return REG_SAFARI.test(userAgent) && !REG_CHROME.test(userAgent)
}
/**
* 检查浏览器是否已经启用 flash 插件
* 注意:
* 1. 如果浏览器禁用了插件,则无法检查
* 2. 谷歌、火狐、微软Edge、Safari等现代浏览器不支持ActiveXObject,它们支持navigator.plugins检查浏览器插件
* 3. chrome浏览器默认是禁用flash插件的
*/
export function isFlash() {
let flash
if (typeof window.ActiveXObject != 'undefined') {
flash = window.ActiveXObject('ShockwaveFlash.ShockwaveFlash')
} else {
flash = navigator.plugins['Shockwave Flash']
}
return flash ? true : false
}
export function isNull(value) {
const type = Object.prototype.toString
return type.call(value) === '[object Null]'
}
export function isUndefined(value) {
const type = Object.prototype.toString
return type.call(value) === '[object Undefined]'
}
export function isBoolean(value) {
const type = Object.prototype.toString
return type.call(value) === '[object Boolean]'
}
export function isString() {
const type = Object.prototype.toString
return type.call(value) === '[object String]'
}
export function isNumber(value) {
const type = Object.prototype.toString
return type.call(value) === '[object Number]'
}
export function isNaN(value) {
const type = Object.prototype.toString
return type.call(value) === '[object NaN]'
}
/**
* 是否是一个对象类型
* @param {*} value
*/
export function isObject(value) {
const type = Object.prototype.toString
return type.call(value) === '[object Object]'
}
/**
* 是否是一个数组
* @param {Array} value
*/
export function isArray (value) {
const type = Object.prototype.toString
return type.call(value) === '[object Array]'
}
export function isRegExp(value) {
const type = Object.prototype.toString
return type.call(value) === '[object RegExp]'
}
export function isMap(value) {
const type = Object.prototype.toString
return type.call(value) === '[object Map]'
}
export function isPromise(value) {
const type = Object.prototype.toString
return type.call(value) === '[object Promise]'
}
export function isDate(value) {
const type = Object.prototype.toString
reutrn type.call(value) === '[object Date]'
}
export function isMath(value) {
const type = Object.prototype.toString
return type.call(value) === '[object Math]'
}
export function isJSON(value) {
const type = Object.prototype.toString
return type.call(value) === '[object JSON]'
}
检测 Error
export function isError(value) {
const type = Object.prototype.toString
return type.call(value) === '[object Error]'
}
/**
* html 过滤为 text(即去除html标签)
* @param {String} value html
*/
export function htmlToText(value) {
const REG_HTML_TO_TEXT = /<\/?.+?\/?>/g
return value.replace(REG_HTML_TO_TEXT, '')
}
/**
* 生成UUID
*/
export function createUuid() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
const r = Math.random() * 16 | 0
const v = c == 'x' ? r : (r & 0x3 | 0x8)
return v.toString(16)
})
}
/**
* 时间过滤器
* @param {String} value 时间字符串
*/
export function moment() {
var datetime = new Date()
if (typeof value === 'string' && /^[0-9]{4}(\-|\/)[0-9]{1,2}(\-|\/)[0-9]{1,2}/.test(value)) {
datetime = new Date(value.replace(/\-/g, '/'))
} else if (Object.prototype.toString.call(value) === '[object Date]') {
datetime = value
}
function repair(value) {
return value > 9 ? value : `0${value}`
}
function format(type) {
var datetime_format,
Y = datetime.getFullYear(),
M = datetime.getMonth() + 1,
D = datetime.getDate(),
H = datetime.getHours(),
m = datetime.getMinutes(),
s = datetime.getSeconds()
switch (type) {
case 'YYYY-MM':
datetime_format = `${Y}-${repair(M)}`
break;
case 'YYYY-MM-DD':
datetime_format = `${Y}-${repair(M)}-${repair(D)}`
break;
case 'HH:mm':
datetime_format = `${repair(H)}:${repair(m)}`
break;
case 'HH:mm:ss':
datetime_format = `${repair(H)}:${repair(m)}:${repair(s)}`
break;
case 'YYYY-MM-DD HH:mm':
datetime_format = `${Y}-${repair(M)}-${repair(D)} ${repair(H)}:${repair(m)}`
break;
case 'YYYY-MM-DD HH:mm:ss':
datetime_format = `${Y}-${repair(M)}-${repair(D)} ${repair(H)}:${repair(m)}:${repair(s)}`
break;
case 'YYYY/MM':
datetime_format = `${Y}/${repair(M)}`
break;
case 'YYYY/MM/DD':
datetime_format = `${Y}/${repair(M)}/${repair(D)}`
break;
case 'YYYY/MM/DD HH:mm':
datetime_format = `${Y}/${repair(M)}/${repair(D)} ${repair(H)}:${repair(m)}`
break;
case 'YYYY/MM/DD HH:mm:ss':
datetime_format = `${Y}/${repair(M)}/${repair(D)} ${repair(H)}:${repair(m)}:${repair(s)}`
break;
default:
break;
}
}
return {
format
}
}
// 用法
moment().format('YYYY-MM-DD HH:mm:ss')
moment('2020-12-12 23:23:24').format('HH:mm:ss')
moment('2020/12/12 23:23:24').format('YYYY-MM')