/** * 用户输入内容验证类 */ // 是否为空 export const isEmpty = (str) => { return str.trim() == '' } /** * 匹配phone */ export const isPhone = (str) => { const reg = /^((0\d{2,3}-\d{7,8})|(1[3456789]\d{9}))$/ return reg.test(str) } /** * 匹配phone */ export const isMobile = (str) => { const reg = /^(1[3456789]\d{9})$/ return reg.test(str) } /** * 匹配Email地址 */ export const isEmail = (str) => { if (str == null || str == "") return false var result = str.match(/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/) if (result == null) return false return true } /** * 判断数值类型,包括整数和浮点数 */ export const isNumber = (str) => { if (isDouble(str) || isInteger(str)) return true return false } /** * 判断是否为正整数(只能输入数字[0-9]) */ export const isPositiveInteger = (str) => { return /(^[0-9]\d*$)/.test(str) } /** * 匹配integer */ export const isInteger = (str) => { if (str == null || str == "") return false var result = str.match(/^[-\+]?\d+$/) if (result == null) return false return true } /** * 匹配double或float */ export const isDouble = (str) => { if (str == null || str == "") return false var result = str.match(/^[-\+]?\d+(\.\d+)?$/) if (result == null) return false return true } // 判断的是否是JSON字符串 export const isJson = (str) => { if (typeof str == 'string') { try { var obj = JSON.parse(str); // 等于这个条件说明就是JSON字符串 会返回true if (typeof obj == 'object' && obj) { return true; } else { //不是就返回false return false; } } catch (e) { return false; } } return false; } /** * 根据身份证获取出生年月日 * @param {String} IdNO - 身份证号 * @return {Number} 日期 */ export const getBirthdayByIdNO = (IdNO) => { let birthday = ""; if (IdNO.length == 18) { birthday = IdNO.substr(6, 8); return birthday.replace(/(.{4})(.{2})/, "$1-$2-"); } else if (IdNO.length == 15) { birthday = "19" + IdNO.substr(6, 6); return birthday.replace(/(.{4})(.{2})/, "$1-$2-"); } else { return ""; } } /** * 根据身份证获取出性别 * @param {String} IdNO - 身份证号 * @return {Number} 0 女 , 1 男 ,未识别成功 男 */ export const getSexByIdNO = (IdNO) => { if (IdNO.length == 18) { return IdNO.charAt(16) % 2 == 0 ? '0' : '1'; } else if (IdNO.length == 15) { return IdNO.charAt(14) % 2 == 0 ? '0' : '1'; } else { return '1'; } }