-
Notifications
You must be signed in to change notification settings - Fork 1
/
signup-form.html
293 lines (251 loc) · 7.3 KB
/
signup-form.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Signup Form</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.14.0/css/all.min.css" integrity="sha512-1PKOgIY59xJ8Co8+NE6FZ+LOAZKjy+KY8iq0G4B3CyeY6wYHN3yt9PW0XpSriVlkMXe40PTKnXrLnZ9+fkDaog==" crossorigin="anonymous" />
<style>
/* layout */
body {
display: grid;
margin: 0;
padding: 0;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
height: 100vh;
}
aside {
/* hide on mobile only, see desktop styles below */
display: none;
background-image: url("https://dhhl0lw17ed7f.cloudfront.net/static/ice-cream.svg");
background-size: cover;
background-position: 50%;
width: 100%;
height: 100%;
}
main {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
/* form */
form {
width: 400px;
box-shadow: 0 15px 35px rgba(50, 50, 93, .1), 0 5px 15px rgba(0, 0, 0, .07);
border-radius: 4px;
}
.dkh-form-header {
display: flex;
justify-content: center;
text-align: center;
padding: 24px;
padding-bottom: 48px;
}
.dkh-form-header small,
.dkh-form-body small {
display: block;
margin-bottom: 16px;
color: #8898aa;
}
.dkh-form-header__social-wrapper {
display: flex;
}
.dkh-form-header__social-wrapper .dkh-btn-github {
margin-right: 16px;
}
.dkh-form-body {
flex-direction: column;
display: flex;
align-items: center;
padding: 48px 48px 16px;
}
.dkh-form-body,
.dkh-form-footer {
background-color: #f4f5f7;
}
fieldset {
border: none;
width: 100%;
padding: 0;
}
.dkh-form-field {
width: 100%;
margin-bottom: 8px;
}
.dkh-form-field__input {
width: 100%;
padding: 12px;
box-sizing: border-box;
box-shadow: 0 1px 3px rgba(50,50,93,.15), 0 1px 0 rgba(0,0,0,.02);
border: 1px solid white;
border-radius: 4px;
}
.dkh-form-field__input:focus {
outline: none;
border: 1px solid #3a86ff;
}
.dkh-form-field__messages {
margin-top: 4px;
color: red;
font-size: 14px;
height: 16px;
}
.dkh-form-footer {
display: flex;
justify-content: center;
padding-bottom: 48px;
}
/* buttons */
.dkh-btn {
cursor: pointer;
display: inline-block;
font-weight: 600;
color: #525f7f;
text-align: center;
vertical-align: middle;
user-select: none;
background-color: white;
border: 1px solid transparent;
padding: 12px 24px;
border-radius: 4px;
transition: all .15s ease;
box-shadow: 0 4px 6px rgba(50, 50, 93, .11), 0 1px 3px rgba(0, 0, 0, .08);
text-transform: uppercase;
}
.dkh-btn:focus {
outline: none;
}
.dkh-btn:hover {
background-color: #ececec;
}
.dkh-btn-primary {
background-color: #3a86ff;
border-color: #3a86ff;
color: white;
}
.dkh-btn-primary:hover {
background-color: #2176ff;
}
.dkh-btn-icon {
display: flex;
justify-content: center;
}
.dkh-btn-icon i {
margin-right: 12px;
}
.dkh-btn-github i {
color: #172459;
}
.dkh-btn-twitter i {
color: #1ca2f1;
}
/* desktop styles */
@media screen and (min-width: 1024px) {
body {
grid-template-columns: 40% 60%;
}
aside {
display: inline;
}
}
</style>
</head>
<body>
<aside></aside>
<main>
<form id="dkh-signup-form">
<div class="dkh-form-header">
<div>
<small>Sign up with</small>
<div class="dkh-form-header__social-wrapper">
<button type="button" class="dkh-btn dkh-btn-icon dkh-btn-github">
<i class="fab fa-github fa-lg"></i>
Github
</button>
<button type="button" class="dkh-btn dkh-btn-icon dkh-btn-twitter">
<i class="fab fa-twitter fa-lg"></i>
Twitter
</button>
</div>
</div>
</div>
<div class="dkh-form-body">
<small>Or sign in with email and password</small>
<div class="dkh-form-field">
<fieldset class="email">
<input autofocus class="dkh-form-field__input" type="text" name="email" id="email" placeholder="Email">
<div class="dkh-form-field__messages"></div>
</fieldset>
</div>
<div class="dkh-form-field">
<fieldset class="password">
<input class="dkh-form-field__input" name="password" type="password" id="password" placeholder="Password">
<div class="dkh-form-field__messages"></div>
</fieldset>
</div>
</div>
<div class="dkh-form-footer">
<button class="dkh-btn dkh-btn-primary" type="submit">Sign Up</button>
</div>
</form>
</main>
<script>
// store signup form element reference and add submit event listener
const signupForm = document.getElementById('dkh-signup-form');
signupForm.addEventListener('submit', handleSignupFormSubmit);
/**
* callback function for signup form submit event listener
*/
function handleSignupFormSubmit(e) {
// prevent default browser behaviour
e.preventDefault();
const formDataEntries = new FormData(signupForm).entries();
const { email, password } = Object.fromEntries(formDataEntries);
const emailErrorMessage = validateEmail(email);
const passowrdErrorMessage = validatePassword(password);
if (emailErrorMessage) {
// select the email form field message element
const emailErrorMessageElement = document.querySelector('.email .dkh-form-field__messages');
// show email error message to user
emailErrorMessageElement.innerText = emailErrorMessage;
}
if (passowrdErrorMessage) {
// select the email form field message element
const passwordErrorMessageElement = document.querySelector('.password .dkh-form-field__messages');
// show password error message to user
passwordErrorMessageElement.innerText = passowrdErrorMessage;
}
}
/**
* validate email
*/
function validateEmail(email) {
if (!email) return 'Email is required';
const isValidEmail = /^\S+@\S+$/g
if (!isValidEmail.test(email)) {
return 'Please enter a valid email';
}
return '';
}
/**
* validate passowrd
*/
function validatePassword(password, minlength) {
if (!password) return 'Password is required';
if (password.length < minlength) {
return `Please enter a password that's at least ${minlength} characters long`;
}
const hasCapitalLetter = /[A-Z]/g;
if (!hasCapitalLetter.test(password)) {
return 'Please use at least one capital letter.';
}
const hasNumber = /\d/g;
if (!hasNumber.test(password)) {
return 'Please use at least one number.';
}
return '';
}
</script>
</body>
</html>