1. 检验数据是否为空
const isEmpty = obj => {
if (obj === '' || obj === null || obj === undefined) {
return true
} else if (obj.constructor === Array && obj.length === 0) {
return true
} else if (obj.constructor === Object && Object.keys(obj).length === 0) {
return true
} else {
return false
}
}
2. 检验手机号
const phone = mobile => {
var tel = /^1[3|4|5|6|7|8|9]\d{9}$/
return tel.test(mobile)
}
3. 判断两个对象是否相等
function isObjectValueEqual(a, b) {
// 取对象a和b的属性名
let aProps = Object.getOwnPropertyNames(a)
let bProps = Object.getOwnPropertyNames(b)
// 判断属性名的length是否一致
if (aProps.length != bProps.length) {
return false
}
// 循环取出属性名,再判断属性值是否一致
for (var i = 0; i < aProps.length; i++) {
var propName = aProps[i]
if (a[propName] !== b[propName]) {
return false
}
}
return true
}
4. 邮箱正则验证
function checkEmail(value) {
const regEmail = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/;
return regEmail.test(value)
}