password utils

mail@pastecode.io avatar
unknown
typescript
a month ago
678 B
5
Indexable
Never
export const requirements = [
  { re: /[0-9]/, label: 'Include numeri' },
  { re: /[a-z]/, label: 'Include lettere minuscole' },
  { re: /[A-Z]/, label: 'Include lettere maiuscole' },
  { re: /[$&+,:;=?@#|'<>.^*()%!-]/, label: 'Include caratteri speciali' },
]
export function getStrength(password: string) {
  let multiplier = password.length > 5 ? 0 : 1

  requirements.forEach(requirement => {
    if (!requirement.re.test(password)) {
      multiplier += 1
    }
  })

  return Math.max(100 - (100 / (requirements.length + 1)) * multiplier, 10)
}
export const passwordRegex =
  /^(?=[^A-Z\n]*[A-Z])(?=[^a-z\n]*[a-z])(?=[^0-9\n]*[0-9])(?=[^#?!@$%^&*\n-]*[#?!@$%^&*-]).{8,}$/
Leave a Comment