52 lines
1.6 KiB
JavaScript
52 lines
1.6 KiB
JavaScript
/**
|
|
* 校验电话号码
|
|
* 支持:大陆手机号、大陆固话、400电话、港澳手机号
|
|
* @param {string} phone
|
|
* @returns {boolean}
|
|
*/
|
|
export function isValidPhone(phone) {
|
|
if (!phone) return false
|
|
// 大陆手机号
|
|
const mainlandMobile = /^1[3-9]\d{9}$/
|
|
// 大陆固话:区号(可选) + 7~8位号码
|
|
const mainlandLandline = /^(0\d{2,3}-?)\d{7,8}$/
|
|
// 400电话
|
|
const phone400 = /^400-?\d{3}-?\d{4}$/
|
|
// 港澳手机号(8位,首位5/6/8/9)
|
|
const hkMacaoMobile = /^[5689]\d{7}$/
|
|
// 港澳固话
|
|
const hkMacaoLandline = /^(\d{4}-?)?\d{4}$/
|
|
|
|
return (
|
|
mainlandMobile.test(phone) ||
|
|
mainlandLandline.test(phone) ||
|
|
phone400.test(phone) ||
|
|
hkMacaoMobile.test(phone) ||
|
|
hkMacaoLandline.test(phone)
|
|
)
|
|
}
|
|
|
|
/**
|
|
* 校验时段格式 HH:mm 及起止关系
|
|
* @param {string} startTime
|
|
* @param {string} endTime
|
|
* @returns {{valid: boolean, message?: string}}
|
|
*/
|
|
export function validateTimeRange(startTime, endTime) {
|
|
const timeRegex = /^([0-1]\d|2[0-3]):([0-5]\d)$/
|
|
if (!startTime || !endTime) {
|
|
return { valid: false, message: '请填写完整的起止时段' }
|
|
}
|
|
if (!timeRegex.test(startTime) || !timeRegex.test(endTime)) {
|
|
return { valid: false, message: '时段格式不正确,应为 00:00 ~ 23:59' }
|
|
}
|
|
const [startH, startM] = startTime.split(':').map(Number)
|
|
const [endH, endM] = endTime.split(':').map(Number)
|
|
const start = startH * 60 + startM
|
|
const end = endH * 60 + endM
|
|
if (start > end) {
|
|
return { valid: false, message: '开始时间不得晚于结束时间' }
|
|
}
|
|
return { valid: true }
|
|
}
|