Skip to content

Commit

Permalink
feat: add some regular expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
suressk committed Jul 22, 2022
1 parent 90c8380 commit ebe4a85
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/regex/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* email address
*/
export const RegEx_Email = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/

/**
* domain name
*/
export const RegEx_Domain = /[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+\.?/

/**
* (weak) password: 6~18 bits, contain: `letters` / `numbers` / `_`
* and must start with letters
*/
export const RegEx_Password = /^[a-zA-Z]\w{5,17}$/

/**
* strong password: 8~16 bits, contain: `letters` and `numbers`
* Special characters cannot be used
* 必须包含大小写字母和数字的组合,不能使用特殊字符,长度在8-16之间
*/
export const RegEx_StrongPassword = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,16}$/

/**
* phone number
*/
export const RegEx_PhoneNumber = /^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/
35 changes: 35 additions & 0 deletions src/regex/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {
RegEx_Domain,
RegEx_Email,
RegEx_Password,
RegEx_PhoneNumber,
RegEx_StrongPassword
} from './constants'

/**
* Check val is domain name
* @param val the check val
* @returns boolean
*/
export const isDomainName = (val: string): boolean => RegEx_Domain.test(val)
/**
* Check val is email
* @param val the check val
* @returns boolean
*/
export const isEmail = (val: string): boolean => RegEx_Email.test(val)

/**
* Check val is phone number
* @param val the check val
* @returns boolean
*/
export const isPhoneNumber = (val: string): boolean => RegEx_PhoneNumber.test(val)

export {
RegEx_Domain,
RegEx_Email,
RegEx_Password,
RegEx_PhoneNumber,
RegEx_StrongPassword
}

0 comments on commit ebe4a85

Please sign in to comment.