refactor: 提取正则和验证到 @vben/utils
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
export * from './constants';
|
||||
export * from './helpers';
|
||||
export * from './validator';
|
||||
export * from '@vben-core/shared/cache';
|
||||
export * from '@vben-core/shared/color';
|
||||
export * from '@vben-core/shared/utils';
|
||||
|
||||
2
packages/utils/src/validator/index.ts
Normal file
2
packages/utils/src/validator/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from './regex';
|
||||
export * from './validator';
|
||||
18
packages/utils/src/validator/regex.ts
Normal file
18
packages/utils/src/validator/regex.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
/** 手机号正则表达式(中国) */
|
||||
const MOBILE_REGEX = /(?:0|86|\+86)?1[3-9]\d{9}/;
|
||||
/** 身份证号正则表达式 */
|
||||
const ID_CARD_REGEX = /^\d{15}|\d{18}$/;
|
||||
/** 邮箱正则表达式 */
|
||||
const EMAIL_REGEX = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
|
||||
/** 密码正则表达式 以字母开头,长度在6~18之间,只能包含字母、数字和下划线 */
|
||||
const PASSWORD_REGEX = /^[a-z]\w{5,17}$/i;
|
||||
/** 强密码 必须包含大小写字母和数字的组合,不能使用特殊字符,长度在8-10之间 */
|
||||
const STRONG_PASSWORD_REGEX = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$/;
|
||||
|
||||
export {
|
||||
EMAIL_REGEX,
|
||||
ID_CARD_REGEX,
|
||||
MOBILE_REGEX,
|
||||
PASSWORD_REGEX,
|
||||
STRONG_PASSWORD_REGEX,
|
||||
};
|
||||
16
packages/utils/src/validator/validator.ts
Normal file
16
packages/utils/src/validator/validator.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { MOBILE_REGEX } from './regex';
|
||||
|
||||
/**
|
||||
* 验证是否为手机号码(中国)
|
||||
*
|
||||
* @param value 值
|
||||
* @returns 是否为手机号码(中国)
|
||||
*/
|
||||
function isMobile(value?: null | string): boolean {
|
||||
if (!value) {
|
||||
return false;
|
||||
}
|
||||
return MOBILE_REGEX.test(value);
|
||||
}
|
||||
|
||||
export { isMobile };
|
||||
Reference in New Issue
Block a user