Files
xk-client-wx/utils/phone.js
李琦 c681cb916b feat:
1. 手机号格式校验
2026-08-11 18:39:38 +08:00

39 lines
1.1 KiB
JavaScript
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.
/**
* 大陆手机号校验(与后端 UtilsService::checkPhone 一致)
* 规则11 位,号段 13-19^1[3456789]\d{9}$
*/
const MOBILE_REG = /^1[3456789]\d{9}$/;
/**
* 判断是否为合法大陆手机号(不弹 toast
* @param {string|number|null|undefined} phone
* @returns {boolean}
*/
export function isPhone(phone) {
if (phone === null || phone === undefined) {
return false;
}
return MOBILE_REG.test(String(phone).trim());
}
/**
* 校验手机号;不合法时 toast 并返回 false
* @param {string|number|null|undefined} phone
* @param {string} [emptyMsg='请输入手机号']
* @param {string} [invalidMsg='手机号格式不正确']
* @returns {boolean}
*/
export function assertPhone(phone, emptyMsg = '请输入手机号', invalidMsg = '手机号格式不正确') {
const value = phone === null || phone === undefined ? '' : String(phone).trim();
if (!value) {
uni.showToast({ title: emptyMsg, icon: 'none' });
return false;
}
if (!MOBILE_REG.test(value)) {
uni.showToast({ title: invalidMsg, icon: 'none' });
return false;
}
return true;
}