开发常用的校验方法
const idCode = /(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9X]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}$)/;
const validateNull = (rule, value, callback) => {
if (rule.required && (!value || value.length === 0)) {
callback(rule.tip ? rule.tip : false);
} else {
callback();
}
};
const validateContact = (rule, value, callback) => {
if (rule.required && !value) {
callback('请填写');
} else if (value && !(/^[0-9-]{7,50}$/.test(value))) {
callback('请填写正确的');
} else {
callback();
}
};
const validatePhone = (rule, value, callback) => {
if (rule.required && !value) {
callback('请填写');
} else if (value && !(/^[1]([3-9])\d{9}$/.test(value))) {
callback('请填写正确的');
} else {
callback();
}
};
const validateElectronicPhone = (rule, value, callback) => {
if (rule.required && !value) {
callback('请填写');
} else if (value && !(/^[0-9]{11}$/.test(value))) {
callback('请填写正确的');
} else {
callback();
}
};
const validateEmail = (rule, value, callback) => {
if (rule.required && !value) {
callback('请填写');
} else if (value && !(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/.test(value))) {
callback('请填写正确的');
} else {
callback();
}
};
const validateFax = (rule, value, callback) => {
if (rule.required && !value) {
callback('请填写');
} else if (value && !(/^([0-9]{2,3}-)?([0-9]{3,4}-)?([0-9]{7,8})$/.test(value))) {
callback('请填写正确的');
} else {
callback();
}
};
const validateAge = (rule, value, callback) => {
if (rule.required && !value) {
callback('请填写');
} else if (value && !(/^[1-9]\d?$|^1[0-2]\d$/.test(value))) {
callback('请填写正确的');
} else {
callback();
}
};
const validateIdCard = (rule, value, callback) => {
if (rule.required && !value) {
callback('请填写');
} else if (value && !(idCode.test(value))) {
callback('请填写正确的');
} else {
callback();
}
};
const validateHMID = (rule, value, callback) => {
if (rule.required && !value) {
callback('请填写');
} else if (value && !(/^[HM]{1}([0-9]{10}|[0-9]{8})$/.test(value))) {
callback('请填写正确的');
} else {
callback();
}
};
const validatePostcode = (rule, value, callback) => {
if (rule.required && !value) {
callback('请填写');
} else if (value && !(/^\d{6}$/.test(value))) {
callback('请填写正确的');
} else {
callback();
}
};
export function setCookie (cName, value, expiredays) {
if (value === null || value === undefined || value === '' || value === 'null' || value === 'undefined') {
return false
}
var exdate = new Date()
exdate.setTime(exdate.getTime() + (expiredays || 1800000))
var v4reg = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/
var v6reg = /^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/gm
let hostName = window.location.hostname
let domain
if (hostName === 'localhost' || v4reg.test(hostName) || v6reg.test(hostName)) {
domain = hostName
} else {
let hostArr = hostName.split('.')
domain = hostArr.length >= 3 ? ('.' + hostArr[hostArr.length - 3] + '.' + hostArr[hostArr.length - 2] + '.' + hostArr[hostArr.length - 1]) : hostName
}
document.cookie = cName + '=' + value + ';expires=' + exdate.toGMTString() + ';Domain=' + domain + ';path=/'
}
export function removeCookie (cName) {
setCookie(cName, 0, -1 * 1800000)
}
export function clearAllCookie () {
var keys = document.cookie.match(/[^ =;]+(?==)/g)
if (keys) {
for (var i = keys.length; i--;) {
setCookie(keys[i], 0, -1 * 1800000)
}
}
};
export function transObj2Array (source, level) {
let lvl = level === undefined ? 1 : +level
let result = []
switch (+lvl) {
case 1:
source === null ? result = [] : result.push(source)
break
case 2:
result = {}
for (let obj in source) {
if (source[obj] === null || source[obj] === undefined) {
result[obj] = []
} else if (source[obj] instanceof Array) {
result[obj] = source[obj]
} else {
result[obj] = [source[obj]]
}
}
break
}
return result
};
export function formatTime (fmt, date) {
if (!date) {
return ''
}
date = new Date(date)
const o = {
'M+': date.getMonth() + 1,
'd+': date.getDate(),
'h+': date.getHours(),
'm+': date.getMinutes(),
's+': date.getSeconds(),
'q+': Math.floor((date.getMonth() + 3) / 3),
'S': date.getMilliseconds()
}
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
}
for (var k in o) {
if (new RegExp('(' + k + ')').test(fmt)) {
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
}
}
return fmt
};
export function fmObj2Arr (data) {
let arr = []
for (let key in data) {
arr.push({
name: data[key],
value: key
})
}
return arr
}