-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassword_controller.js
68 lines (54 loc) · 1.63 KB
/
password_controller.js
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
57
58
59
60
61
62
63
64
65
66
67
68
import { Controller } from "@hotwired/stimulus"
export default class extends Controller {
static targets = [ 'password' ]
verify() {
const element = document.querySelector('div.password-explanation')
this.imageSwitch
if (this.passwordIsValid) {
element.classList.add('valid')
} else {
element.classList.remove('valid')
}
}
get password() {
return this.passwordTarget.value
}
get passwordIsValid() {
return (
this.hasValidLength &&
this.hasUpperCaseCharacter &&
this.hasLowerCaseCharacter &&
this.hasSpecialCharacter
)
}
get hasValidLength() {
return this.password.length >= 8 && this.password.length <= 72
}
get hasUpperCaseCharacter() {
return this.password.toLowerCase() != this.password;
}
get hasLowerCaseCharacter() {
return this.password.toUpperCase() != this.password;
}
get hasSpecialCharacter() {
const specialChars = /[#?!@$%^&*-]/;
return specialChars.test(this.password);
}
get imageSwitch() {
const element1 = document.getElementById('password-img-1')
const element2 = document.getElementById('password-img-2')
const element3 = document.getElementById('password-img-3')
const element4 = document.getElementById('password-img-4')
this.imageSrc(this.hasValidLength, element1)
this.imageSrc(this.hasUpperCaseCharacter, element2)
this.imageSrc(this.hasLowerCaseCharacter, element3)
this.imageSrc(this.hasSpecialCharacter, element4)
}
imageSrc(conditon, imageElement) {
if (conditon) {
imageElement.src = '/accept.png'
} else {
imageElement.src = '/cancel.png'
}
}
}