cpms_operation_platform/src/utils/form.ts

48 lines
1.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 表单正则校验工具
*
* 只导出纯正则常量与独立的校验函数,不做任何 antd/framework 耦合。
* 各页面的 Form.Item rules 应在对应页面目录的 controller.ts 中组装。
*/
// ============================================================
// 正则常量
// ============================================================
/**
* 中国大陆手机号
* - 11 位数字,第二位 3-9
*/
export const CHINA_PHONE_REGEX = /^1[3-9]\d{9}$/;
/**
* 登录密码
* - 至少 8 位,同时包含字母与数字
* - 允许特殊字符 !@#$%^&*()_+-=
*/
export const LOGIN_PASSWORD_REGEX = /^(?=.*[A-Za-z])(?=.*\d)[\w!@#$%^&*()_+\-=]{8,}$/;
/**
* 真实姓名(中文/英文/数字1-8 位)
*/
export const REAL_NAME_REGEX = /^[\u4e00-\u9fa5A-Za-z0-9·]{1,8}$/;
// ============================================================
// 校验函数
// ============================================================
/** 校验中国大陆手机号 */
export function isChinaPhone(value: unknown): boolean {
return typeof value === 'string' && CHINA_PHONE_REGEX.test(value);
}
/** 校验密码:至少 8 位 + 同时含字母与数字 */
export function isValidPassword(value: unknown): boolean {
return typeof value === 'string' && LOGIN_PASSWORD_REGEX.test(value);
}
/** 校验真实姓名1-8 个字符 */
export function isValidRealName(value: unknown): boolean {
return typeof value === 'string' && value.length > 0 && REAL_NAME_REGEX.test(value);
}