1. Mailbox verification
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)
}
Copy the code
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 to 8] \ d {8} $| ^ 18 [\ d] {9} $| ^ 19 [8, 9] \ d {8} $/
function validateEmail(str) {
const regex = new RegExp(EMAIL)
return regex.test(str)
}
Copy the code
3, a space
Space before and after detection
Use the trim attribute of the Input component (on by default)
const TRIM = /^\S$|^\S.*\S$/
export function validateTrim(str) {
const regex = new RegExp(TRIM)
return regex.test(str)
}
Copy the code
4, password -passoword
Example: Contains at least six characters and contains at least two combinations of digits, letters, and 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)
}
Copy the code
5, website
Example: starts 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)
}
Copy the code
Id card number
Example: All lowercase letters and digits must start with a letter or digit and can contain hyphens (-), underscores (_), periods (.), and slashes (/)
const CODE_LOWER = /^[a-z0-9][a-z0-9-_./]*$/
export function validateCodeLower(str) {
const regex = new RegExp(CODE_LOWER)
return regex.test(str)
}
Copy the code
7. Passport Number
const PASSPORT = / ^ 1 [45] [0-9] {7} $| (^ [P | | S | S] P \ d {7} $) | (^ [S | S | G | G | E | E] \ d {8} $) | (^ [Gg | | Ss | Ll Tt | | Qq Dd | | Aa Ff] \ d {8} $) | (^ | | | H M M] [H \ d {8, 10} $) /
export function validatePassport(str) {
const regex = new RegExp(PASSPORT)
return regex.test(str)
}
Copy the code
8, coding, case restriction
Example: Case and digit must start with a letter or digit and can contain hyphens (-), underscores (_), periods (.), and slashes ()
const CODE = /^[a-zA-Z0-9][a-zA-Z0-9-_./]*$/
export function validateCode(str) {
const regex = new RegExp(CODE)
return regex.test(str)
}
Copy the code
9. Non-+ 86 mobile phone number – can contain digits only
const NOT_CHINA_PHONE = /^\d+$/
export function validateNotChinaPhone(str) {
const regex = new RegExp(NOT_CHINA_PHONE)
return regex.test(str)
}
Copy the code