1. Mailbox
const EMAIL = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/
function validatePhone(str) { const regex = new RegExp(PHONE) return regex.test(str) } 2. Mobile phone number
const PHONE = /^134[0-8]\d{7}$|^13[^4]\d{8}$|^14[5-9]\d{8}$|^15[^4]\d{8}$|^16[6]\d{8}$|^17[0-8]\d{8}$|^18[\d]{9}$|^19[8,9]\d{8}$/
function validateEmail(str) { const regex = new RegExp(EMAIL) return regex.test(str) } 3. Spaces Before and after spaces are detected, it is recommended to use the trim attribute of the Input component (enabled by default)
const TRIM = /^\S$|^\S.*\S$/
export function validateTrim(str) { const regex = new RegExp(TRIM) return regex.test(str) } 4. Example of password-passoword : contains at least 2 combinations of numbers/letters/characters, with a length of at least 6 characters
const PASSWORD = /^(?![0-9]+$)(?![a-zA-Z]+$)(?![a-z]+$)(?![!@#$%^&*=]+$)[0-9A-Za-z!@#$%^&*=]{6,}$/
export function validatePassword(str) { const regex = new RegExp(PASSWORD) return regex.test(str) } 5. URL Example: start with https/http
const STRICT_URL = /(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\\.,@?^=%&:/~\\+#]*[\w\-\\@?^=%&/~\\+#])?/
export function validateStrictUrl(str) { const regex = new RegExp(STRICT_URL) return regex.test(str) } 6. Example of ID number : all lowercase and numbers. It must start with letters and numbers, and can include -, _, ., /Wait
const CODE_LOWER = /^[a-z0-9][a-z0-9-_./]*$/
export function validateCodeLower(str) {
const regex = new RegExp(CODE_LOWER)
return regex.test(str)
}
7、护照号码
const PASSPORT = /^1[45][0-9]{7}$|(^[P|p|S|s]\d{7}$)|(^[S|s|G|g|E|e]\d{8}$)|(^[Gg|Tt|Ss|Ll|Qq|Dd|Aa|Ff]\d{8}$)|(^[H|h|M|m]\d{8,10}$)/
export function validatePassport(str) { const regex = new RegExp(PASSPORT) return regex.test(str) } 8. Examples of encoding and case restrictions : uppercase and lowercase and numbers must start with letters and numbers, and can include -, _, .,/
const CODE = /^[a-zA-Z0-9][a-zA-Z0-9-_./]*$/
export function validateCode(str) { const regex = new RegExp(CODE) return regex.test(str) } 9. Non-+86 mobile phone number-only and only numbers
const NOT_CHINA_PHONE = /^\d+$/
export function validateNotChinaPhone(str) {
const regex = new RegExp(NOT_CHINA_PHONE)
return regex.test(str)
}