You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
3.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/**
* 数据验证工具
*
* 功能:验证用户输入的数据格式是否正确
*/
/**
* 验证邮箱格式
* @param {string} email - 邮箱地址
* @returns {boolean} 是否有效
*/
const validateEmail = (email) => {
// 正则表达式:匹配标准邮箱格式
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailRegex.test(email);
};
/**
* 验证中国大陆手机号
* @param {string} phone - 手机号
* @returns {boolean} 是否有效
*/
const validatePhone = (phone) => {
// 正则表达式1开头第二位是3-9后面9位数字
const phoneRegex = /^1[3-9]\d{9}$/;
return phoneRegex.test(phone);
};
/**
* 验证必填项
* @param {*} value - 要验证的值
* @param {string} fieldName - 字段名称
* @returns {object} { valid: boolean, message: string }
*/
const validateRequired = (value, fieldName = '该字段') => {
if (value === null || value === undefined || value === '') {
return {
valid: false,
message: `${fieldName}不能为空`
};
}
return {
valid: true,
message: ''
};
};
/**
* 验证密码强度
* @param {string} password - 密码
* @returns {object} { valid: boolean, message: string }
*
* 要求至少6位包含字母和数字
*/
const validatePassword = (password) => {
if (!password || password.length < 6) {
return {
valid: false,
message: '密码长度至少6位'
};
}
// 至少包含一个字母和一个数字
const hasLetter = /[a-zA-Z]/.test(password);
const hasNumber = /\d/.test(password);
if (!hasLetter || !hasNumber) {
return {
valid: false,
message: '密码必须包含字母和数字'
};
}
return {
valid: true,
message: ''
};
};
/**
* 验证用户名
* @param {string} username - 用户名
* @returns {object} { valid: boolean, message: string }
*
* 要求3-20位只能包含字母、数字、下划线
*/
const validateUsername = (username) => {
if (!username || username.length < 3 || username.length > 20) {
return {
valid: false,
message: '用户名长度应为3-20位'
};
}
const usernameRegex = /^[a-zA-Z0-9_]+$/;
if (!usernameRegex.test(username)) {
return {
valid: false,
message: '用户名只能包含字母、数字和下划线'
};
}
return {
valid: true,
message: ''
};
};
/**
* 验证数字范围
* @param {number} value - 数值
* @param {number} min - 最小值
* @param {number} max - 最大值
* @param {string} fieldName - 字段名称
* @returns {object} { valid: boolean, message: string }
*/
const validateNumberRange = (value, min, max, fieldName = '该值') => {
const num = Number(value);
if (isNaN(num)) {
return {
valid: false,
message: `${fieldName}必须是数字`
};
}
if (num < min || num > max) {
return {
valid: false,
message: `${fieldName}应在${min}-${max}之间`
};
}
return {
valid: true,
message: ''
};
};
/**
* 批量验证
* @param {array} validations - 验证规则数组
* @returns {object} { valid: boolean, errors: array }
*
* 使用示例:
* const result = validateBatch([
* validateRequired(username, '用户名'),
* validateEmail(email, '邮箱')
* ]);
*/
const validateBatch = (validations) => {
const errors = validations
.filter(v => !v.valid)
.map(v => v.message);
return {
valid: errors.length === 0,
errors
};
};
// 导出所有函数
module.exports = {
validateEmail,
validatePhone,
validateRequired,
validatePassword,
validateUsername,
validateNumberRange,
validateBatch
};