/
index.html
executable file
·120 lines (110 loc) · 4.82 KB
/
index.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
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<div id="blockform">
<form method="POST" action="saveuser.php" id="form">
<div class="row error center" id="blockerror"></div>
<div class="row">
<div class="firstcolumn">Name</div>
<div class="secondcolumn"><input type="text" name="name" id="name" class="inputfield" /></div>
</div>
<div class="row">
<div class="firstcolumn">CoName</div>
<div class="secondcolumn"><input type="text" name="coname" id="coname" class="inputfield" /></div>
</div>
<div class="row">
<div class="firstcolumn">Email</div>
<div class="secondcolumn"><input type="email" name="mail" id="mail" class="inputfield" /></div>
</div>
<div class="row">
<div class="firstcolumn">Password</div>
<div class="secondcolumn"><input type="password" name="pass" id="pass" class="inputfield" /></div>
</div>
<div class="row">
<div class="firstcolumn">Repeat password</div>
<div class="secondcolumn"><input type="password" name="pass2" id="pass2" class="inputfield" /></div>
</div>
<div class="row center">
<input type="button" value="Register" id="savebutton" />
</div>
</form>
</div>
<script>
let error=[];
document.addEventListener("DOMContentLoaded", () => {
document.getElementById('savebutton').onclick = function() {
let error=[];
let post = [];
let pass='';
Array.from(document.getElementsByClassName('inputfield')).forEach((elem) => {
let value = document.getElementById(elem.getAttribute('id')).value;
post.push(elem.getAttribute('name')+'='+value);
switch(elem.getAttribute('type')) {
case 'text':
if (!validLen(value)) {
error.push('field ' + elem.getAttribute('name') + ' must have from 3 to 30 symbols');
}
break;
case 'email':
if (!validLen(value)) {
error.push('field ' + elem.getAttribute('name') + ' must have from 3 to 30 symbols');
}
if (!validEmail(value)) {
error.push('field' + elem.getAttribute('name') + ' must have @ symbol');
}
break;
case 'password':
if (!validPass(value)) {
error.push('field' + elem.getAttribute('name') + ' must have from 8 symbols');
}
if (pass == '') {
pass = value;
} else if (!validRepeat(pass, value)) {
error.push('pass and pass2 is not identical');
}
break;
}
});
if (error.length > 0) {
error.forEach((elem) => {
document.getElementById('blockerror').innerHTML = document.getElementById('blockerror').innerHTML + '<div>' + elem + '</div>';
});
} else {
document.getElementById('blockerror').innerHTML = '';
register(post);
}
};
});
function validLen(val) {
return val.trim().length >= 3 && val.trim().length <= 30;
}
function validEmail(val) {
return val.indexOf('@') >= 0;
}
function validPass(val) {
return val.length >= 8 && val.length <= 30;
}
function validRepeat(val1, val2) {
return val1 == val2;
}
function register(post) {
const xhr = new XMLHttpRequest();
xhr.open('POST', document.getElementById('form').getAttribute('action'));
xhr.responseType = 'json';
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.onload = () => {
console.log(xhr.response);
if (xhr.status !== 201) {
document.getElementById('blockerror').innerHTML = '<div>' + xhr.response.error + '</div>';
return;
} else {
document.getElementById('blockform').innerHTML='<div>'+xhr.response.data+'</div>';
}
}
xhr.send(post.join('&'));
}
</script>
</body>
</html>