20 lines
593 B
TypeScript
20 lines
593 B
TypeScript
const MOBILE_PHONE_RE = /^1[3-9]\d{9}$/;
|
|
|
|
export function normalizePhoneInput(value: string): string {
|
|
return value.replace(/\D/g, '').slice(0, 11);
|
|
}
|
|
|
|
export function validateMobilePhone(phone: string): { ok: boolean; message?: string } {
|
|
const trimmed = phone.trim();
|
|
if (!trimmed) {
|
|
return { ok: false, message: '请输入手机号码' };
|
|
}
|
|
if (trimmed.length !== 11) {
|
|
return { ok: false, message: '手机号码须为 11 位' };
|
|
}
|
|
if (!MOBILE_PHONE_RE.test(trimmed)) {
|
|
return { ok: false, message: '请输入正确的手机号码' };
|
|
}
|
|
return { ok: true };
|
|
}
|