-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinscription.php
160 lines (123 loc) · 4.22 KB
/
inscription.php
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
<?php
require_once 'images/config_projet.php';
require_once 'tools.php';
require_once 'templates/pdo.php';
require_once 'user.php';
require_once 'templates/header.php';
$errors = [];
$messages = [];
if (isset($_POST['addUser'])) {
$res = addUser($pdo, $_POST['first_name'], $_POST['last_name'], $_POST['email'], $_POST['password']);
if ($res) {
$messages[] = 'Merci pour votre inscription';
} else {
$errors[] = 'Une erreur s\'est produite lors de votre inscription';
}
}
?>
<h1>Inscription</h1>
<?php foreach ($messages as $message) { ?>
<div class="alert alert-success" role="alert">
<?= $message; ?>
</div>
<?php } ?>
<?php foreach ($errors as $error) { ?>
<div class="alert alert-danger" role="alert">
<?= $error; ?>
</div>
<?php } ?>
<form method="POST" id='lessonForm' >
<div class="mb-3">
<label for="first_name" class="form-label">Prénom</label>
<input type="text" class="form-control" id="first_name" name="first_name">
</div>
<div class="mb-3">
<label for="last_name" class="form-label">Nom</label>
<input type="text" class="form-control" id="last_name" name="last_name">
</div>
<div class="mb-3">
<label for="email" class="form-label">Email</label>
<input type="email" class="form-control" id="email" name="email">
</div>
<div class="mb-3">
<label for="password" class="form-label">Mot de psse</label>
<input type="password" class="form-control" id="password" name="password">
</div>
<input type="submit" name="addUser" class="btn btn-primary" value="Enregistrer">
</form>
<script> (function() {
'use strict'
let form = document.getElementById('lessonForm');
form.addEventListener('submit', function(event) {
Array.from(form.elements).forEach((input) => {
if (input.type !== "submit") {
if (!validateFields(input)) {
alert('1')
event.preventDefault();
event.stopPropagation();
input.classList.remove("is-valid");
input.classList.add("is-invalid");
}
else {
alert('3')
input.classList.remove("is-invalid");
input.classList.add("is-valid");
}
}
});
}, false)
})()
function validateRequired(input) {
return !(input.value == null || input.value == "");
}
function validateLength(input, minLength, maxLength) {
return !(input.value.length < minLength || input.value.length > maxLength);
}
function validateText(input) {
return input.value.match("^[A-Za-z]+$");
}
function validateEmail(input) {
let EMAIL = input.value;
let POSAT = EMAIL.indexOf("@");
let POSDOT = EMAIL.lastIndexOf(".");
return !(POSAT < 1 || (POSDOT - POSAT < 2));
}
function validateFields(input) {
let fieldName = input.name; }
if (fieldName == "first_name") {
if (!validateRequired(input)) {
return false;
}
if (!validateLength(input, 2, 20)) {
return false;
}
if (!validateText(input)) {
return false;
}
return true;
}
if (fieldName == "last_name") {
if (!validateRequired(input)) {
return false;
}
if (!validateLength(input, 2, 20)) {
return false;
}
if (!validateText(input)) {
return false;
}
return true;
}
if (fieldName == "email") {
if (!validateRequired(input)) {
return false;
}
if (!validateEmail(input)) {
return false;
}
return true;
}
</script>
<?php
require_once 'templates/footer.php';
?>