-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathform.js
More file actions
132 lines (103 loc) · 3.84 KB
/
form.js
File metadata and controls
132 lines (103 loc) · 3.84 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
document.addEventListener("DOMContentLoaded", () => {
/*
* select elements from login and sign up form
* changes signup form or login form to active
*/
const signUp = document.getElementById("sign-up");
const logIn = document.getElementById("login");
const container = document.querySelector(".container");
signUp.addEventListener("click", () => {
container.classList.add("active");
});
logIn.addEventListener("click", () => {
container.classList.remove("active");
});
/*
* event listener that allows the user to toggle username or email
* for signing in
*/
const loginType = document.getElementById("login-type");
const userName = document.getElementById("username");
const emailAdd = document.getElementById("email-add");
loginType.addEventListener("change", function() {
userName.style.display = "none";
emailAdd.style.display = "none";
if(this.value === "text"){
userName.style.display = "block";
}
else if(this.value === "email"){
emailAdd.style.display = "block";
}
});
/*
* Validations
*/
const signupForm = document.getElementById("signup-form");
// signupForm.addEventListener("submit", function(e) {
// e.preventDefault();
// document
// .querySelectorAll(".error-message")
// .forEach(el => {
// el.style.display = none
// const user = document.getElementById("user").value.trim();
// const email = document.getElementById("email").value.trim();
// const password = document.getElementById("password").value.trim();
// let valid = true;
// if(!isValidUser){
// valid = false;
// document.getElementById("userError").style.display = "block";
// }
// if(!isValidEmail){
// valid = false;
// document.getElementById("emailError").style.display = "block";
// }
// if(!isValidPassword){
// valid = false;
// document.getElementById("passwordError").style.display = "block";
// }
// if(valid){
// alert("New account created!");
// this.submit();
// }
// });
// });
const errors = document.querySelectorAll(".error-message");
Array.from(errors).forEach(errorMessage => {
errorMessage.style.display = "none";
});
signupForm.addEventListener("submit", (e) => {
e.preventDefault();
const user = document.getElementById("user").value.trim();
const email = document.getElementById("email").value.trim();
const password = document.getElementById("password").value.trim();
let valid = true;
if(!isValidUser(user)){
valid = false;
document.getElementById("userError").style.display = "block";
}
if(!isValidEmail(email)){
valid = false;
document.getElementById("emailError").style.display = "block";
}
if(!isValidPassword(password)){
valid = false;
document.getElementById("passwordError").style.display = "block";
}
if(valid){
alert("New account created!");
this.submit();
}
})
function isValidUser(user){
const re =/^[a-zA-Z0-9_-]{4,16}$/;
return re.test(user);
}
function isValidEmail(email){
const re =/^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return re.test(email);
}
function isValidPassword(password){
const re =/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,}$/;
return re.test(password);
}
});