-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA_REGEX_NO_REPEATED_CHARACTER.html
More file actions
138 lines (137 loc) · 6.36 KB
/
A_REGEX_NO_REPEATED_CHARACTER.html
File metadata and controls
138 lines (137 loc) · 6.36 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>REGEX NO REPEATED CHARACTER - STEP 21</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
<style>
.is-Invalid{
border: 2px solid red;
background-color: #ffb1b14d;
}
.is-Valid{
border: 2px solid green;
}
.error-message{
color: red;
background-color: #ffb1b1;
padding: 3px 0px 3px 0px;
font-weight: 900;
}
</style>
</head>
<body>
<h1><b>REGEX NO REPEATED CHARACTER - STEP 21</b></h1>
<div class="container mt-5">
<form id="myForm" novalidate>
<div class="form-group">
<label for="pwdInput">Password</label>
<input type="text" class="form-control" id="pwdInput" placeholder="Enter Password">
<br />
<span id="pwdError" class="error-message"></span>
</div>
<br />
<button type="submit" class="btn btn-primary" id="submitBtn">Submit</button>
</form>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.min.js" integrity="sha384-0pUGZvbkm6XF6gxjEnlmuGrJXVbNuzT9qBBavbLwCsOGabYfZo0T0to5eqruptLy" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
<script>
//----------------------------------------------------------------
const validationMessage = {
Password: {
LengthMsg: "Password must be at least 8 characters long",
repeatMsg: "Not contain any repeated characters.",
uppercase: "Password must include at least one uppercase letter (A–Z).",
lowercase: "Password must include at least one lowercase letter (a–z).",
Number: "Password must include at least one number (0–9).",
special: "Password must include at least one special character (e.g. -, !, @, #, $, %, ^, &, _, .).",
valids: "Strong password! Looks good."
}
};
//----------------------------------------------------------------
//----------------------------------------------------------------
LiveValidate({
input: document.getElementById("pwdInput"),
regex: /^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[!@#$%^&*_\.-])(?!.*(.).*\1)[A-Za-z0-9!@#$%^&*_\.-]{8,}$/i,
error: document.getElementById("pwdError"),
message: validationMessage.Password,
type: "PWD"
});
//----------------------------------------------------------------
//----------------------------------------------------------------
function LiveValidate({input, regex, error, message, type}){
input.addEventListener("input", () => {
if(type === "PWD"){
const lenghtCheck = input.value.length >= 8;
const numberCheck = /\d/.test(input.value);
const upperCase = /[A-Z]/.test(input.value);
const lowerCase = /[a-z]/.test(input.value);
const specialChar = /[-!@#$%^&_\.]/.test(input.value);
const repeatCheck = /^(?!.*(.).*\1).+$/.test(input.value);
if(!lenghtCheck)
{
error.innerText = message.LengthMsg,
input.classList.add("is-Invalid"),
input.classList.remove("is-Valid")
}
else if(!repeatCheck)
{
error.innerText = message.repeatMsg,
input.classList.add("is-Invalid"),
input.classList.remove("is-Valid")
}
else if(!numberCheck)
{
error.innerText = message.Number,
input.classList.add("is-Invalid"),
input.classList.remove("is-Valid")
}
else if(!upperCase)
{
error.innerText = message.uppercase,
input.classList.add("is-Invalid"),
input.classList.remove("is-Valid")
}
else if(!lowerCase)
{
error.innerText = message.lowerCase,
input.classList.add("is-Invalid"),
input.classList.remove("is-Valid")
}
else if(!specialChar)
{
error.innerText = message.special,
input.classList.add("is-Invalid"),
input.classList.remove("is-Valid")
}
else
{
error.innerText = "",
input.classList.remove("is-Invalid"),
input.classList.add("is-Valid")
}
}
});
}
//----------------------------------------------------------------
//----------------------------------------------------------------
document.getElementById("myForm").addEventListener("submit", function(e){
e.preventDefault();
const input = document.getElementById("pwdInput");
const error = document.getElementById("pwdError");
input.dispatchEvent(new Event("input"));
if(input.classList.contains("is-Invalid") || input.value.trim() === ""){
error.innerText = validationMessage.url.message;
input.classList.add("is-Invalid");
input.classList.remove("is-Valid");
input.focus();
return;
}
alert("FORM SUBMIT SUCCESSFULLY!");
});
//----------------------------------------------------------------
</script>
</body>
</html>