diff --git a/apps/web-antd/src/adapter/form.ts b/apps/web-antd/src/adapter/form.ts index 7b43f201a..d5eb21b89 100644 --- a/apps/web-antd/src/adapter/form.ts +++ b/apps/web-antd/src/adapter/form.ts @@ -7,10 +7,7 @@ import type { ComponentType } from './component'; import { setupVbenForm, useVbenForm as useForm, z } from '@vben/common-ui'; import { $t } from '@vben/locales'; - -// TODO @xingyu:要不搞到全局的校验? -/** 手机号正则表达式(中国) */ -const MOBILE_REGEX = /(?:0|86|\+86)?1[3-9]\d{9}/; +import { isMobile } from '@vben/utils'; async function initSetupVbenForm() { setupVbenForm({ @@ -45,7 +42,7 @@ async function initSetupVbenForm() { mobile: (value, _params, ctx) => { if (value === undefined || value === null || value.length === 0) { return true; - } else if (!MOBILE_REGEX.test(value)) { + } else if (!isMobile(value)) { return $t('ui.formRules.mobile', [ctx.label]); } return true; @@ -55,7 +52,7 @@ async function initSetupVbenForm() { if (value === undefined || value === null || value.length === 0) { return $t('ui.formRules.required', [ctx.label]); } - if (!MOBILE_REGEX.test(value)) { + if (!isMobile(value)) { return $t('ui.formRules.mobile', [ctx.label]); } return true; diff --git a/apps/web-ele/src/adapter/form.ts b/apps/web-ele/src/adapter/form.ts index 362ec798f..fd36adc5a 100644 --- a/apps/web-ele/src/adapter/form.ts +++ b/apps/web-ele/src/adapter/form.ts @@ -7,9 +7,7 @@ import type { ComponentType } from './component'; import { setupVbenForm, useVbenForm as useForm, z } from '@vben/common-ui'; import { $t } from '@vben/locales'; - -// TODO @xingyu:要不搞到全局的校验? -const MOBILE_REGEX = /(?:0|86|\+86)?1[3-9]\d{9}/; +import { isMobile } from '@vben/utils'; async function initSetupVbenForm() { setupVbenForm({ @@ -36,7 +34,7 @@ async function initSetupVbenForm() { mobile: (value, _params, ctx) => { if (value === undefined || value === null || value.length === 0) { return true; - } else if (!MOBILE_REGEX.test(value)) { + } else if (!isMobile(value)) { return $t('ui.formRules.mobile', [ctx.label]); } return true; @@ -46,7 +44,7 @@ async function initSetupVbenForm() { if (value === undefined || value === null || value.length === 0) { return $t('ui.formRules.required', [ctx.label]); } - if (!MOBILE_REGEX.test(value)) { + if (!isMobile(value)) { return $t('ui.formRules.mobile', [ctx.label]); } return true; diff --git a/apps/web-ele/src/utils/index.ts b/apps/web-ele/src/utils/index.ts index 4e65303a0..da4b8f04e 100644 --- a/apps/web-ele/src/utils/index.ts +++ b/apps/web-ele/src/utils/index.ts @@ -4,5 +4,4 @@ export * from './formatTime'; export * from './formCreate'; export * from './rangePickerProps'; export * from './routerHelper'; -export * from './validator'; export { CommonStatusEnum } from '@vben/utils'; diff --git a/apps/web-ele/src/utils/validator.ts b/apps/web-ele/src/utils/validator.ts deleted file mode 100644 index 86ae018e6..000000000 --- a/apps/web-ele/src/utils/validator.ts +++ /dev/null @@ -1,18 +0,0 @@ -// 参数校验,对标 Hutool 的 Validator 工具类 -// TODO @xingyu:要不要抽到 package 里? - -/** 手机号正则表达式(中国) */ -const MOBILE_REGEX = /(?:0|86|\+86)?1[3-9]\d{9}/; - -/** - * 验证是否为手机号码(中国) - * - * @param value 值 - * @returns 是否为手机号码(中国) - */ -export function isMobile(value?: null | string): boolean { - if (!value) { - return false; - } - return MOBILE_REGEX.test(value); -} diff --git a/apps/web-naive/src/adapter/form.ts b/apps/web-naive/src/adapter/form.ts index 19fba4f89..c782f5f47 100644 --- a/apps/web-naive/src/adapter/form.ts +++ b/apps/web-naive/src/adapter/form.ts @@ -7,8 +7,7 @@ import type { ComponentType } from './component'; import { setupVbenForm, useVbenForm as useForm, z } from '@vben/common-ui'; import { $t } from '@vben/locales'; - -const MOBILE_REGEX = /(?:0|86|\+86)?1[3-9]\d{9}/; +import { isMobile } from '@vben/utils'; async function initSetupVbenForm() { setupVbenForm({ @@ -39,7 +38,7 @@ async function initSetupVbenForm() { mobile: (value, _params, ctx) => { if (value === undefined || value === null || value.length === 0) { return true; - } else if (!MOBILE_REGEX.test(value)) { + } else if (!isMobile(value)) { return $t('ui.formRules.mobile', [ctx.label]); } return true; @@ -49,7 +48,7 @@ async function initSetupVbenForm() { if (value === undefined || value === null || value.length === 0) { return $t('ui.formRules.required', [ctx.label]); } - if (!MOBILE_REGEX.test(value)) { + if (!isMobile(value)) { return $t('ui.formRules.mobile', [ctx.label]); } return true; diff --git a/apps/web-naive/src/utils/validator.ts b/apps/web-naive/src/utils/validator.ts deleted file mode 100644 index 3ae62f787..000000000 --- a/apps/web-naive/src/utils/validator.ts +++ /dev/null @@ -1,17 +0,0 @@ -// 参数校验,对标 Hutool 的 Validator 工具类 - -/** 手机号正则表达式(中国) */ -const MOBILE_REGEX = /(?:0|86|\+86)?1[3-9]\d{9}/; - -/** - * 验证是否为手机号码(中国) - * - * @param value 值 - * @returns 是否为手机号码(中国) - */ -export function isMobile(value?: null | string): boolean { - if (!value) { - return false; - } - return MOBILE_REGEX.test(value); -} diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts index b1d2831ff..fa73eb86d 100644 --- a/packages/utils/src/index.ts +++ b/packages/utils/src/index.ts @@ -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'; diff --git a/packages/utils/src/validator/index.ts b/packages/utils/src/validator/index.ts new file mode 100644 index 000000000..0d7fc2954 --- /dev/null +++ b/packages/utils/src/validator/index.ts @@ -0,0 +1,2 @@ +export * from './regex'; +export * from './validator'; diff --git a/packages/utils/src/validator/regex.ts b/packages/utils/src/validator/regex.ts new file mode 100644 index 000000000..9d1e7c47a --- /dev/null +++ b/packages/utils/src/validator/regex.ts @@ -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, +}; diff --git a/packages/utils/src/validator/validator.ts b/packages/utils/src/validator/validator.ts new file mode 100644 index 000000000..862f33017 --- /dev/null +++ b/packages/utils/src/validator/validator.ts @@ -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 };