刘光辉
7 小时以前 0dfe84494048ce27ba8449831782128412d3eb13
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<script setup lang="ts">
import { computed } from 'vue';
 
const props = withDefaults(defineProps<{ password?: string }>(), {
  password: '',
});
 
const strengthList: string[] = ['', '#e74242', '#ED6F6F', '#EFBD47', '#55D18780', '#55D187'];
 
const currentStrength = computed(() => {
  return checkPasswordStrength(props.password);
});
 
const currentColor = computed(() => {
  return strengthList[currentStrength.value];
});
 
/**
 * Check the strength of a password
 */
function checkPasswordStrength(password: string) {
  let strength = 0;
 
  // Check length
  if (password.length >= 8) strength++;
 
  // Check for lowercase letters
  if (/[a-z]/.test(password)) strength++;
 
  // Check for uppercase letters
  if (/[A-Z]/.test(password)) strength++;
 
  // Check for numbers
  if (/\d/.test(password)) strength++;
 
  // Check for special characters
  if (/[^\da-z]/i.test(password)) strength++;
 
  return strength;
}
</script>
 
<template>
  <div class="relative mt-2 flex items-center justify-between">
    <template v-for="index in 5" :key="index">
      <div class="dark:bg-input-background bg-heavy relative mr-1 h-1.5 w-1/5 rounded-sm last:mr-0">
        <span
          :style="{
            backgroundColor: currentColor,
            width: currentStrength >= index ? '100%' : '',
          }"
          class="absolute left-0 h-full w-0 rounded-sm transition-all duration-500"></span>
      </div>
    </template>
  </div>
</template>