| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- /**
- * 表单验证工具
- */
- /**
- * 验证手机号
- */
- export const validatePhone = (phone: string): boolean => {
- const phoneReg = /^1[3-9]\d{9}$/
- return phoneReg.test(phone)
- }
- /**
- * 验证邮箱
- */
- export const validateEmail = (email: string): boolean => {
- const emailReg = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/
- return emailReg.test(email)
- }
- /**
- * 验证身份证号
- */
- export const validateIdCard = (idCard: string): boolean => {
- const idCardReg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
- return idCardReg.test(idCard)
- }
- /**
- * 验证密码强度
- * 必须包含:数字、大小写字母、特殊字符,长度至少6位
- */
- export const validatePassword = (password: string): boolean => {
- // 长度至少6位
- if (password.length < 6) {
- return false
- }
-
- // 必须包含数字
- const hasNumber = /[0-9]/.test(password)
- // 必须包含小写字母
- const hasLowerCase = /[a-z]/.test(password)
- // 必须包含大写字母
- const hasUpperCase = /[A-Z]/.test(password)
- // 必须包含特殊字符
- const hasSpecialChar = /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]/.test(password)
-
- return hasNumber && hasLowerCase && hasUpperCase && hasSpecialChar
- }
- /**
- * 验证是否为空
- */
- export const validateRequired = (value: string): boolean => {
- return value != null && value.trim().length > 0
- }
- /**
- * 验证数字
- */
- export const validateNumber = (value: string): boolean => {
- const numberReg = /^[0-9]+(\.[0-9]+)?$/
- return numberReg.test(value)
- }
- /**
- * 验证整数
- */
- export const validateInteger = (value: string): boolean => {
- const integerReg = /^[0-9]+$/
- return integerReg.test(value)
- }
- /**
- * 验证字符串长度范围
- */
- export const validateLength = (value: string, min: number, max: number): boolean => {
- const length = value.length
- return length >= min && length <= max
- }
- /**
- * 验证 URL
- */
- export const validateUrl = (url: string): boolean => {
- const urlReg = /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/
- return urlReg.test(url)
- }
|