-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
130 lines (94 loc) · 3.02 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Smart Home Manager - Log-in</title>
<link rel="stylesheet" href="css/style.css" media="screen" type="text/css" />
</head>
<body>
<div class="login-card">
<h1 id="title">Smart Home Manager</h1><br>
<h1>Log-in</h1><br>
<form id="login-form" action="login_mongodb.php" method="post">
<input type="text" name="mail" placeholder="Username">
<span id="email-error"></span>
<input type="password" name="pass" placeholder="Password">
<span id="pass-error"></span>
<input type="submit" name="login" class="login login-submit" value="login">
</form>
<div class="login-help">
<a href="register.html">Register</a> • <a href="#">Forgot Password</a>
</div>
</div>
<script>
/*
** Regex for various fields **
*/
// Alphanumeric, underscores and hyphen 3 to 16 chars long
var re_username = new RegExp(/^[\w_\-]{3,16}$/);
// All characters except quotes greater than 8 characters
var re_password = new RegExp(/^[^'"]{8,}$/);
var re_email = new RegExp(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/);
/*
** Selectors for various fields **
*/
var passwordTxt = document.getElementsByName('pass')[0];
var emailTxt = document.getElementsByName('mail')[0];
/*
** Error elements for various fields **
*/
var usernameErr = document.getElementById('username-error');
var passwordErr = document.getElementById('pass-error');
var emailErr = document.getElementById('email-error');
/*
** Flags for correctness various fields **
*/
var usernameChk = false;
var passwordChk = false;
var emailChk = false;
/*
** Validation functions **
*/
function toggleError(errorInputField, errorMsgField, hasError, errorMsg){
if(errorInputField.value.trim().length == 0) {
hasError = true;
errorMsg = "Required field";
}
if(hasError) {
errorMsgField.classList.add('error-text');
errorMsgField.innerText = errorMsg;
errorInputField.classList.add('error-field');
} else {
errorMsgField.innerText = "";
errorMsgField.classList.remove('error-text');
errorInputField.classList.remove('error-field');
}
}
function validatePassword(e) {
passwordChk = re_password.test(passwordTxt.value);
errorMsg = "Passwords must be at least 8 characters long and must not contain any quotes";
toggleError(passwordTxt, passwordErr, !passwordChk, errorMsg)
}
function validateEmail(e) {
emailTxt.value = emailTxt.value.trim();
emailChk = re_email.test(emailTxt.value);
errorMsg = "Invalid Email";
toggleError(emailTxt, emailErr, !emailChk, errorMsg);
}
/*
** Event Listeners **
*/
passwordTxt.addEventListener("blur", validatePassword);
emailTxt.addEventListener("blur", validateEmail);
var loginform = document.getElementById('login-form');
loginform.addEventListener("submit", function(e) {
e.preventDefault();
validatePassword();
validateEmail();
if(emailChk && passwordChk)
loginform.submit();
return false;
});
</script>
</body>
</html>