-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwelcome.js
187 lines (141 loc) · 4.24 KB
/
welcome.js
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
$(document).ready(function () {
start();
});
function start() {
$("#signupButtom").click(function (e) { signupFunction(); });
$("#signupMenuButton").click(function (e) { signupFunction(); });
$("#loginByttom").click(function (e) { loginFunction(); });
$("#loginMenuButton").click(function (e) { loginFunction(); });
$("#submit").click(function (e) { submitFunction(); });
$("#connect").click(function (e) { connectFunction(); });
$("#welcomeMenuButton").click(function (e) { welcomeFunction(); });
localStorage.setItem("p", "p");
}
function welcomeFunction() {
//switchDevs
$("#signup").hide();
$("#login").hide();
$("#aboutModal").hide();
$("#defsForm").hide();
$("#game").hide();
$("#welcome").show();
stopGame();
}
function signupFunction() {
//switchDevs
$("#login").hide();
$("#aboutModal").hide();
$("#defsForm").hide();
$("#game").hide();
$("#welcome").hide();
$("#signup").show();
stopGame();
}
function loginFunction() {
//switchDevs
$("#aboutModal").hide();
$("#defsForm").hide();
$("#game").hide();
$("#welcome").hide();
$("#signup").hide();
$("#login").show();
stopGame();
}
function submitFunction() {
let username = $("#username").val();
let password = $("#password").val();
let passwordCnfirm = $("#passwordConfirm").val();
let fullname = $("#fullName").val();
let email = $("#email").val();
let birthday = $("#birthday").val();
//add check if username already exsit
if (username == "") {
alert("Username must be filled out");
return false;
}
if (checkUsernameExist(username)) {
alert("Username already exsit");
return false;
}
if (password == "") {
alert("Password must be filled out");
return false;
}
if (!(password.match("^(?=.*[A-Za-z])(?=.*\\d)[A-Za-z\\d^a-zA-Z0-9].{5,50}$")) || password.length < 6) {
alert("Password must be contains at least six charcter , one letter and one number");
return false;
}
if (passwordCnfirm != password) {
alert("Passwords does not math");
return false;
}
if (fullname == "") {
alert("Full name must be filled out");
return false;
}
//maybe full name need to be seprtated to surname and last name
if (/\d/.test(fullname)) {
alert("Full name can not contains number");
return false;
}
if (!ValidateEmail(email)) {
alert("You have entered an invalid email address!")
return false;
}
if (birthday == "") {
alert("Birthday must be filled out")
return false;
}
createUser(username, password);
$("#signup :input").prop("value", '');
//switchDevs
$("#signup").hide();
$("#welcome").show();
}
// storing input from sign in
function createUser(username, password) {
let usernameJSON = JSON.stringify(username);
let passwordJSON = JSON.stringify(password);
localStorage.setItem(username, password);
}
function checkUsernameExist(username) {
let user = localStorage.getItem(username)
if (user == undefined) {
return false;
}
return true;
}
function connectFunction() {
users = localStorage.getItem('users');
let username = $("#usernameConnect").val();
let password = $("#passwordConnect").val();
if (username == "") {
alert("Username must be filled out");
return false;
}
if (password == "") {
alert("Password must be filled out");
return false;
}
if (localStorage.getItem(username) == undefined) {
alert("Username or password are incorrect");
return false;
}
if (localStorage.getItem(username) != password) {
alert("Username or password are incorrect");
return false;
}
//switchDevs
let usernameD = $("#usernameConnect").val();
$("#usernameDefs").empty();
$("#usernameDefs").append("Username: " + "<Strong>" + usernameD + "</Strong>");
$("#login").hide();
$("#defsForm").show();
}
//JavaScript code to validate an email id
function ValidateEmail(email) {
if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)) {
return (true)
}
return (false)
}