-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathI_REGEX_USERNAME_VALIDATION.html
More file actions
79 lines (79 loc) · 3.41 KB
/
I_REGEX_USERNAME_VALIDATION.html
File metadata and controls
79 lines (79 loc) · 3.41 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>REGEX USER_NAME - STEP 13</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 USER_NAME - STEP 13</b></h1>
<div class="container mt-5">
<form id="myForm" novalidate>
<div class="form-group">
<label for="emailInput">UserName</label>
<input type="text" class="form-control" id="nameInput" placeholder="Enter username">
<br />
<span id="nameError" 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 = {
UserName: {
message: "Username must be 5–20 characters long, can include letters, numbers, dots (.) or underscores (_), and cannot start or end with . or _."
}
};
//----------------------------------------------------------------
//----------------------------------------------------------------
LiveValidate({
input: document.getElementById("nameInput"),
regex: /^(?![._])[A-Za-z0-9._]{5,20}(?<![._])$/i,
error: document.getElementById("nameError"),
message: validationMessage.UserName,
type: "UserName"
});
//----------------------------------------------------------------
//----------------------------------------------------------------
function LiveValidate({input, regex, error, message, type}){
input.addEventListener("input", () => {
if(type === "UserName"){
if(!regex.test(input.value))
{
error.innerText = message.message,
input.classList.add("is-Invalid"),
input.classList.remove("is-Valid")
}
else
{
error.innerText = "",
input.classList.remove("is-Invalid"),
input.classList.add("is-Valid")
}
}
});
}
//----------------------------------------------------------------
</script>
</body>
</html>