JS中常用的判断函数


前言

随着项目的积累,将平时用到的函数做了总结,抽出了日常用到的判断函数,仅供大家参考,有问题还望指正,谢谢大家。


一、邮箱

	export const isEmail = (s) => {
	    return /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((.[a-zA-Z0-9_-]{2,3}){1,2})$/.test(s)
	}

二、手机号码

	export const isMobile = (n) => {
	    return /^1[345789][0-9]{9}$/.test(n)
	}

三、URL地址

	export const isURL= (s) => {
		// 协议://(ftp的登录信息)[IP|域名](:端口号)(/或?请求参数)
	    var regStr = '^((https|http|ftp|rtsp|mms)?://)' // (https或http或ftp):// 可有可无
		    + '?(([0-9a-z_!~*\'().&=+$%-]+: )?[0-9a-z_!~*\'().&=+$%-]+@)?' //ftp的user@ 
		    + '(([0-9]{1,3}.){3}[0-9]{1,3}' // IP形式的URL- 199.194.52.184 
		    + '|' // 允许IP和DOMAIN(域名) 
		    + '([0-9a-z_!~*\'()-]+.)*' // 域名- www. 
		    + '([0-9a-z][0-9a-z-]{0,61})?[0-9a-z].' // 二级域名 
		    + '[a-z]{2,6})' // 顶级域名- 1-6位英文
		    + '(:[0-9]{1,4})?' // 端口- :80 1-5位数字
		    + '((/?)|' // url无参数结尾 - 斜杆或这没有
		    + '(/[0-9a-zA-Z_!~*\'().;?:@&=+$,%#-]+)+/?)$' // 请求参数结尾- 英文或数字和[]内的各种字符
	    var reg = new RegExp(regStr)
	    return reg.test(encodeURI(s))
	}

四、身份证

	export const isCardID = (sId) => {
	    if (!/(^\d{15}$)|(^\d{17}(\d|X|x)$)/.test(sId)) {
	        console.log('你输入的身份证长度或格式错误')
	        return false
	    }
	    // 严格的身份证校验
	    // 身份证城市
	    const aCity = {
	        11: "北京",
	        12: "天津",
	        13: "河北",
	        14: "山西",
	        15: "内蒙古",
	        21: "辽宁",
	        22: "吉林",
	        23: "黑龙江",
	        31: "上海",
	        32: "江苏",
	        33: "浙江",
	        34: "安徽",
	        35: "福建",
	        36: "江西",
	        37: "山东",
	        41: "河南",
	        42: "湖北",
	        43: "湖南",
	        44: "广东",
	        45: "广西",
	        46: "海南",
	        50: "重庆",
	        51: "四川",
	        52: "贵州",
	        53: "云南",
	        54: "西藏",
	        61: "陕西",
	        62: "甘肃",
	        63: "青海",
	        64: "宁夏",
	        65: "新疆",
	        71: "台湾",
	        81: "香港",
	        82: "澳门",
	        91: "国外"
	    }
	    if (!aCity[parseInt(sId.substr(0, 2))]) {
	        console.log('你的身份证地区非法')
	        return false
	    }
	    // 出生日期验证
	    const sBirthday = (sId.substr(6, 4) 
	    + "-" + Number(sId.substr(10, 2)) + "-" + Number(sId.substr(12, 2))).replace(/-/g, "/"),
	          d = new Date(sBirthday)
	    if (sBirthday !== (d.getFullYear() + "/" + (d.getMonth() + 1) + "/" + d.getDate())) {
	        console.log('身份证上的出生日期非法')
	        return false
	    }
	    // 身份证号码校验
	    let sum = 0,
	        weights = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2],
	        codes = "10X98765432"
	    for (let i = 0; i < sId.length - 1; i++) {
	        sum += sId[i] * weights[i]
	    }
	    const last = codes[sum % 11] // 计算出来的最后一位身份证号码
	    if (sId[sId.length - 1] !== last) {
	        console.log('你输入的身份证号非法')
	        return false
	    }
	    return true
	}

五、数据类型

1.String

	export const isString = (o) => {
	    return Object.prototype.toString.call(o).slice(8, -1) === 'String'
	}

2.Number

	export const isNumber = (o) => {
	    return Object.prototype.toString.call(o).slice(8, -1) === 'Number'
	}

3.Boolean

	export const isBoolean = (o) => {
	    return Object.prototype.toString.call(o).slice(8, -1) === 'Boolean'
	}

4.Function

	export const isFunction = (o) => {
	    return Object.prototype.toString.call(o).slice(8, -1) === 'Function'
	}

5.Null

	export const isNull = (o) => {
	    return Object.prototype.toString.call(o).slice(8, -1) === 'Null'
	}

6.Undefined

	export const isUndefined = (o) => {
	    return Object.prototype.toString.call(o).slice(8, -1) === 'Undefined'
	}

7.Object

	export const isObject = (o) => {
	    return Object.prototype.toString.call(o).slice(8, -1) === 'Object'
	}

8.Array

	export const isArray = (o) => {
	    return Object.prototype.toString.call(o).slice(8, -1) === 'Array'
	}

9.Date

	export const isDate = (o) => {
	    return Object.prototype.toString.call(o).slice(8, -1) === 'Date'
	}

10.RegExp

	export const isRegExp = (o) => {
	    return Object.prototype.toString.call(o).slice(8, -1) === 'RegExp'
	}

11.Error

	export const isError = (o) => {
	    return Object.prototype.toString.call(o).slice(8, -1) === 'Error'
	}

12.Symbol

	export const isSymbol = (o) => {
	    return Object.prototype.toString.call(o).slice(8, -1) === 'Symbol'
	}

13.Set

	export const isSet = (o) => { // 是否Set对象
	    return Object.prototype.toString.call(o).slice(8, -1) === 'Set'
	}

六、浏览器

1.微信浏览器

	export const isWeiXin = () => {
	    return !!navigator.userAgent.toLowerCase().match(/microMessenger/i)  // === 'micromessenger'
	}

2.移动端

	export const isDeviceMobile = () => {
	    return /android|webos|iphone|ipod|balckberry/i.test(navigator.userAgent.toLowerCase())
	}

3.QQ浏览器

	export const isQQBrowser = () => {
	    return !!navigator.userAgent.toLowerCase().match(/mqqbrowser|qzone|qqbrowser|qbwebviewtype/i)
	}

4.爬虫

	export const isSpider= () => {
	    return /adsbot|googlebot|bingbot|msnbot|yandexbot|baidubot|robot|careerbot|seznambot|bot|baiduspider|jikespider|symantecspider|scannerlwebcrawler|crawler|360spider|sosospider|sogou web sprider|sogou orion spider/.test(navigator.userAgent.toLowerCase())
	}

5.IOS

	export const isIos = () => {
	    const u = navigator.userAgent
	    if (u.indexOf('Android') > -1 || u.indexOf('Linux') > -1) {  // 安卓手机
	        return false
	    } else if (u.indexOf('iPhone') > -1) { // 苹果手机
	        return true
	    } else if (u.indexOf('iPad') > -1) { // iPad
	        return false
	    } else if (u.indexOf('Windows Phone') > -1) { // winPhone手机
	        return false
	    } else {
	        return false
	    }
	}

6.PC

	export const isPC = () => { // 是否为PC端
	    const u = navigator.userAgent
	    const Agents = ['Android', 'iPhone', 'SymbianOS', 'Windows Phone', 'iPad', 'iPod']
	    let flag = true
	    for (let v = 0; v < Agents.length; v++) {
	        if (u.indexOf(Agents[v]) > 0) {
	            flag = false
	            break
	        }
	    }
	    return flag
	}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值