39 lines
1.1 KiB
JavaScript
39 lines
1.1 KiB
JavaScript
/**
|
||
* 大陆手机号校验(与后端 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;
|
||
}
|