1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
|
import lodash from './lodash' const { get } = lodash export const regexValidate = { userName: /^[a-zA-Z0-9_@\-\.]{4,20}$/, password: /^.{8,32}$/, phone: /^1(3|4|5|6|7|8|9)\d{9}$/, landline: /^(([0-9]{3,4}-)?|([0-9]{3,4})?)[0-9]{7,8}$/, identityCard: /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/, gt0: /^(([1-9]\d*(\.\d{1,2})?)|(0\.\d{1}[1-9]))$/, gt0f: /^(?!(0[0-9]{0,}$))[0-9]{1,}[.]{0,}[0-9]{0,}$/, gteq0: /^(\d+)(\.\d{1,2})?$/, number: /^(-?\d+)(\.\d{1,2})?$/, httpUrl: /^(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,}))\.?)(?::\d{2,5})?(?:[/?#]\S*)?$/i }
export function gtNum({ num, message }, value, callback) { if (!(value > num)) { callback(new Error(message || `必须大于${num}`)) } else { callback() } }
export function ltNum({ num, message }, value, callback) { if (!(value < num)) { callback(new Error(message || `必须小于${num}`)) } else { callback() } }
export function gtEqNum({ num, message }, value, callback) { if (!(value >= num)) { callback(new Error(message || `必须大于等于${num}`)) } else { callback() } }
export function ltEqNum({ num, message }, value, callback) { if (!(value <= num)) { callback(new Error(message || `必须小于等于${num}`)) } else { callback() } }
export function validDig({ dig = 2, message }, value, callback) { const reg = dig ? `^(-?\\d+)(\\.\\d{1,${dig}})?$` : `^(-?\\d+)$` const msgdefault = dig ? `小数位数不能超过${dig}位` : '数值必须为整数' var re1 = new RegExp(reg) if (!re1.test(value)) { callback(new Error(message || msgdefault)) } else { callback() } }
export function validNumber({ message }, value, callback) { var re1 = /^(-?\d+)(\.\d+)?$/ if (!re1.test(value)) { callback(new Error(message || `必须是数字`)) } else { callback() } }
export function validNumArea({ gt, lt, gtEq, ltEq, dig }, value, callback) { var mErr = null var mCallback = (err) => { mErr = mErr || err || null } validNumber({}, value, mCallback) validDig({ dig }, value, mCallback) if (gt || gt === 0) { gtNum({ num: gt }, value, mCallback) } if (gtEq || gtEq === 0) { gtEqNum({ num: gtEq }, value, mCallback) } if (lt || lt === 0) { ltNum({ num: lt }, value, mCallback) } if (ltEq || ltEq === 0) { ltEqNum({ num: ltEq }, value, mCallback) } mErr ? callback(mErr) : callback() }
export function isvalidUsername(str) { var uPattern = regexValidate.userName return uPattern.test(str) } export function isvalidPassword(str) { var passwordPattern = regexValidate.password return passwordPattern.test(str) }
export function isExternal(path) { return /^(https?:|mailto:|tel:)/.test(path) } export function isTel({ required }, value, callback) { if (!required && (value === undefined || value.length === 0)) { callback() } if ((value === undefined || value.length === 0) && required) { callback(new Error(`手机号码不能为空`)) } if (!regexValidate.phone.test(value)) { callback(new Error(`手机号码格式错误`)) } else { callback() } } export function isFixTel({ required }, value, callback) { if (!required && (value === undefined || value.length === 0)) { callback() } if ((value === undefined || value.length === 0) && required) { callback(new Error(`固定电话不能为空`)) } if (!/^(\d{3,4}(-|\s)?)?\d{7,14}$/.test(value)) { callback(new Error(`固定电话格式错误`)) } else { callback() } }
export function isNum({ required, text = '内容', max = 15000000, min = -999999, type, th }, value, callback) { max = th && typeof get(th, max) !== 'undefined' ? get(th, max) : max min = th && typeof get(th, min) !== 'undefined' ? get(th, min) : min if (!required && (value === undefined || (value + '').length === 0)) { callback() } if ((value === undefined || (value + '').length === 0) && required) { callback(new Error(`${text}不能为空`)) } if (!/^(-)?\d+(\.\d+)?$/ig.test(value.toString())) { callback(new Error(`${text}必须为数字`)) } else if (value > max) { callback(new Error(`${text}必须不大于${max}`)) } else if (value < min) { callback(new Error(`${text}必须不小于${min}`)) } else if (type === 'money' && !/^(-)?\d+(\.\d{1,2})?$/ig.test(value.toString())) { callback(new Error(`${text}必须是两位以内小数`)) } else { callback() } } export function vin({ num = 0 }, value, callback) { if (value.length < num && value) { callback(new Error(`vin码不能小于${num}位`)) } else { callback() } } export function validateExpressNo({ required }, value, callback) { if (!/^[a-zA-Z0-9]+$/.test(value)) { callback(new Error(`请输入正确格式的快递单号`)) } else { callback() } } export function validateTextLength({ text = '内容', len = 8, required, errText }, value, callback) { if (!required && value === undefined) { callback() return false } if (value === undefined && required) { callback(new Error(`${text}不能为空`)) return false } const cnReg = /([\u4e00-\u9fa5]|[\u3000-\u303F]|[\uFF00-\uFF60])/g const mat = value.match(cnReg) let length = 0 if (mat) { length = mat.length + (value.length - mat.length) * 0.5 } else { length = value.length * 0.5 } if (!required && length === 0) { callback() return false } if (length === 0 && required) { callback(new Error(`${text}不能为空`)) return false } if (length > len && !errText) { callback(new Error(`${text}长度不能超过${len * 2}个字符`)) return false } else if (length > len && errText) { callback(new Error(`${errText}`)) return false } else { callback() return true } }
export function isLatitude({ required }, value, callback) { callback() }
export function isLongitude({ required }, value, callback) { callback() } export function idIdcard({ required }, value, callback) { if (!/(^[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-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}[0-9Xx]$)/.test(value)) { callback(new Error(`请输入正确格式的身份证号码`)) } else { callback() } } export function isAbcNum({ required }, value, callback) { if (!required && !value) { callback() } if (!/^[a-zA-Z0-9]+$/.test(value)) { callback(new Error(`请输入数字或字母`)) } else { callback() } }
export function isPost({ required }, value, callback) { if (!required && !value) { callback() } if (!/^[0-9][0-9]{5}$/.test(value)) { callback(new Error(`请输入正确格式的邮政编码`)) } else { callback() } }
export function isSmpCode({ required }, value, callback) { if (!/^[A-Z]{2}[0-9]{4}$/.test(value)) { callback(new Error(`请输入正确格式的门店编码`)) } else { callback() } }
export function isFrontCode({ required }, value, callback) { if (!/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{3}(\,(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{3})*$/.test(value)) { callback(new Error(`请输入正确格式的端领取串码,多个以逗号分隔`)) } else { callback() } }
|