-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSignUp.js
More file actions
165 lines (145 loc) · 4.64 KB
/
SignUp.js
File metadata and controls
165 lines (145 loc) · 4.64 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
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
//Post
let new_data = {
name : null,
email : null,
password : null
}
let GET = (url) => {
return fetch(url)
.then((res) => res.json())
.then(data => {
return data;
})
.catch((error) => console.log(error));
}
let POST = async (resource , newData) =>{
try{
let res = await fetch(resource, {
method : "POST",
headers : {
'Content-Type' : 'application/json'
},
body : JSON.stringify(newData)
});
if(!res.ok){
console.log("YOu got a problem dude");
return;
}
let data = await res.json();
console.log(data)
}catch(error){
console.log(error)
}
};
// SignUp
let preData ;
let signUpable = false;
let Verified_Name = null;
let Verified_Email = null;
let Verified_Password = null;
let signUpForm = document.getElementById("signUp_form");
let childElements = signUpForm.children;
let childNodes = signUpForm.childNodes;
let warning = document.querySelectorAll(".input_and_warning span");
// SignUp username
let inputted_username = document.getElementById("new_username");
inputted_username.addEventListener("keyup", (event) => {
if(inputted_username.value.length > 2){
let namecheck = preData.filter(user => {
return user.name.toLowerCase() == inputted_username.value.toLowerCase();
});
if(namecheck.length != 0){
warning[0].style.display = "block";
warning[0].style.color = "red";
warning[0].textContent = "User Name has already taken";
}
else{
// Success
warning[0].style.display = "none";
Verified_Name = inputted_username.value.charAt(0).toUpperCase() + inputted_username.value.slice(1);
}
}
else{
warning[0].style.display = "block";
warning[0].style.color = "red";
warning[0].textContent = "Username too short"
}
});
// SignUp email
let inputted_email = document.getElementById("new_email");
inputted_email.addEventListener("keyup", (event) => {
// warning[1].style.display = "none";
if(!inputted_email.value.includes("@")){
warning[1].style.display = "block";
warning[1].textContent = "Require '@' in the address"
}
else{
// Success
warning[1].style.display = "none";
Verified_Email = inputted_email.value;
}
});
// SignUp password
let created_password = null;
let inputted_password = document.getElementById("new_password");
let passowrdFormat =/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@!#])/;
inputted_password.addEventListener("keyup" ,(event) => {
if(inputted_password.value.length > 8){
if(passowrdFormat.test(inputted_password.value)){
warning[2].style.display = "none";
created_password = inputted_password.value;
}
else{
warning[2].style.bottom = "-8px";
warning[2].style.display = "block";
warning[2].textContent = "Capital, small , digit , !@#"
}
}
else{
warning[2].style.bottom = "-22px";
warning[2].style.display = "block";
warning[2].textContent = "Password must be at least 8 characters"
}
});
let confirm_password = document.getElementById("re_password")
confirm_password.addEventListener("keyup" ,() => {
if (created_password) {
if(created_password == confirm_password.value){
// Success
warning[3].style.display = "none";
Verified_Password = created_password;
}
else{
warning[3].style.display = "block";
warning[3].style.bottom = "-22px";
warning[3].textContent = "Still not the same with created password"
}
}
})
window.onload = async () => {
try {
preData = await GET("http://localhost:3001/users");
console.log(preData)
} catch (error) {
console.error("Error fetching data:", error);
}
if(!inputted_username.value){
warning[0].textContent = "Please Enter a name"
}
if(!inputted_email.value){
warning[1].textContent = "Please Enter an email"
}
if(!inputted_email.value){
warning[2].textContent = "Please create password"
}
}
signUpForm.addEventListener("submit", (event) =>{
event.preventDefault();
if(Verified_Name && Verified_Email && Verified_Password){
new_data.name = signUpForm.new_username.value;
new_data.email = signUpForm.new_email.value;
new_data.password = signUpForm.new_password.value;
console.log(new_data)
POST("http://localhost:3001/users", new_data)
}
});