topfans/frontend/utils/validator.js

71 lines
1.5 KiB
JavaScript
Raw Permalink 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.

// 验证手机号格式1开头的11位数字
export function validatePhone(phone) {
const phoneReg = /^1[3-9]\d{9}$/
if (!phone) {
return {
valid: false,
message: '请输入手机号'
}
}
if (!phoneReg.test(phone)) {
return {
valid: false,
message: '请输入正确的手机号格式'
}
}
return {
valid: true,
message: ''
}
}
// 验证密码长度至少6位
export function validatePassword(password) {
if (!password) {
return {
valid: false,
message: '请输入密码'
}
}
if (password.length < 6) {
return {
valid: false,
message: '密码至少为6位'
}
}
return {
valid: true,
message: ''
}
}
// 验证昵称2-20字符中文、英文、数字、下划线首字符不能是数字或下划线
export function validateNickname(nickname) {
if (!nickname) {
return {
valid: false,
message: '请输入昵称'
}
}
// 使用 [...nickname].length 计算字符数(支持中文)
const len = [...nickname].length
if (len < 2 || len > 20) {
return {
valid: false,
message: '昵称长度为2-20字符'
}
}
// 格式校验:中文、英文、数字、下划线,首字符不能是数字或下划线
if (!/^[一-龥a-zA-Z][一-龥a-zA-Z0-9_]*$/.test(nickname)) {
return {
valid: false,
message: '昵称只能使用中文、英文、数字、下划线,首字符不能是数字或下划线'
}
}
return {
valid: true,
message: ''
}
}