forked from Alicunde/HTML
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
88 lines (78 loc) · 2.51 KB
/
script.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
function Password(){
let password = document.querySelectorAll(".Password");
password.active = 0;
for (let s = 0; s < password.length; s++) {
let input = password[s].querySelectorAll("input");
let validation = password[s].querySelectorAll("p");
input = input[0];
let i = password[s].querySelectorAll("i");
for (let b = 0; b < i.length; b++) {
i[b].addEventListener('click', function() {
if(input.type === 'password')input.type="text";
else input.type="password";
});
}
if(validation && input){
PasswordValidation(password[s]);
}
}
}
function PasswordValidation(father){
let input = father.querySelectorAll("input");
input = input[0];
let validation = father.querySelectorAll("p");
console.log(validation);
var letter = validation[0];
var capital = validation[1];
var number = validation[2];
var length = validation[3];
// When the user clicks on the password field, show the message box
input.onfocus = function() {
console.log(validation);
for (let b = 0; b < validation.length; b++)validation[b].classList.add("active");
}
// When the user clicks outside of the password field, hide the message box
input.onblur = function() {
for (let b = 0; b < validation.length; b++)validation[b].classList.remove("active");
}
// When the user starts to type something inside the password field
input.onkeyup = function() {
// Validate lowercase letters
var lowerCaseLetters = /[a-z]/g;
if(input.value.match(lowerCaseLetters)) {
letter.classList.remove("invalid");
letter.classList.add("valid");
} else {
letter.classList.remove("valid");
letter.classList.add("invalid");
}
// Validate capital letters
var upperCaseLetters = /[A-Z]/g;
if(input.value.match(upperCaseLetters)) {
capital.classList.remove("invalid");
capital.classList.add("valid");
} else {
capital.classList.remove("valid");
capital.classList.add("invalid");
}
// Validate numbers
var numbers = /[0-9]/g;
if(input.value.match(numbers)) {
number.classList.remove("invalid");
number.classList.add("valid");
} else {
number.classList.remove("valid");
number.classList.add("invalid");
}
// Validate length
if(input.value.length >= 8) {
length.classList.remove("invalid");
length.classList.add("valid");
} else {
length.classList.remove("valid");
length.classList.add("invalid");
}
}
}
// Run
window.onload = function() { new Password(); };