-
Notifications
You must be signed in to change notification settings - Fork 0
/
adopta.js
76 lines (69 loc) · 1.98 KB
/
adopta.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
function ValidateEmail(inputText)
{
var mailformat = /^\w+([\.-]?\w+)@\w+([\.-]?\w+)(\.\w{2,3})+$/;
if(inputText.match(mailformat))
{
alert("Adresa de mail corecta");
return true;
}
else
{
alert("Nu este o adresa de mail valida");
return false;
}
}
// function CheckPassword(inputtxt)
// {
// var passw = /^(?=.\d)(?=.[a-z])(?=.*[A-Z]).{6,20}$/;
// if(inputtxt.value.match(passw))
// {
// alert('Corect')
// return true;
// }
// else
// {
// alert('Gresit!')
// return false;
// }
// }
window.onload = () => {
const barButton = document.querySelector('.user-bar #changeDetails');
const removeButton = document.querySelector('#removeDetails');
const refreshButton = document.getElementById('refresh');
barButton.addEventListener('click', changeDetails);
removeButton.addEventListener('click', removeDetails);
refreshButton.addEventListener('click', () => {
window.location.reload();
});
renderBar();
}
function renderBar() {
const barMessage = document.querySelector('.user-bar .message');
const barButton = document.querySelector('.user-bar #changeDetails');
let userDetails = null;
try {
userDetails = JSON.parse(localStorage.getItem('userDetails'));
} catch (error) {
userDetails = null;
}
if(userDetails) {
barMessage.innerHTML = `Bine ai venit, ${userDetails.name}! Parola ta este: ${userDetails.age}`;
barButton.innerHTML = "Schimbă Userul";
} else {
barMessage.innerHTML = "Bine ai venit! Conecteaza-te!";
barButton.innerHTML = "LogIn";
}
}
function changeDetails() {
const name = prompt('Introduceti adresa de email');
if(!ValidateEmail(name)) return;
const age = prompt('Introduceti parola');
if(!age) return;
const userDetails = { name, age };
localStorage.setItem('userDetails', JSON.stringify(userDetails));
renderBar();
}
function removeDetails() {
localStorage.removeItem('userDetails');
renderBar();
}