Correction Atelier 5 JS
Correction Atelier 5 JS
<!
DOCTYPE
html
>
<
html
lang
=
"en"
>
<
head
>
<
meta
charset
=
"UTF-8"
>
<
meta
http-equiv
=
"X-UA-Compatible"
content
=
"IE=edge"
>
<
meta
name
=
"viewport"
content
=
"width=device-width,
initial-scale=1.0"
>
<
title
>Exercice 1</
title
>
<
link
rel
=
"stylesheet"
href
=
"ex1.css"
>
</
head
>
<
body
>
<
fieldset
>
<
legend
>Inscription</
legend
>
<
form
id
=
"myform"
>
<
table
>
<
tr
>
<
td
><
label
for
=
"
name"
>Prénom et Nom
*:</
label
> </
td
>
<
td
><
input
type
=
"text"
size
=
"29"
name
=
"name"
id
=
"name"
></
td
>
</
tr
>
<
tr
>
<
td
><
label
for
=
"
cin"
>CIN *:</
label
>
</
td
>
<
td
><
input
type
=
"text"
size
=
"10"
name
=
"cin"
id
=
"cin"
></
td
>
</
tr
>
<
tr
>
<
td
><
label
for
=
"
mail"
>Adresse Email
*:</
label
> </
td
>
<
td
><
input
type
=
"text"
size
=
"29"
name
=
"mail"
id
=
"mail"
></
td
>
</
tr
>
<
tr
>
<
td
><
label
for
=
"
cin"
>Niveau scolaire
:</
label
> </
td
>
<
td
>
<
input
type
=
"radio"
name
=
"niv"
id
=
"niv1"
checked
>Sécondaire
<
input
type
=
"radio"
name
=
"niv"
id
=
"niv2"
>Universitaire
<
input
type
=
"radio"
name
=
"niv"
id
=
"niv3"
>Autre
</
td
>
</
tr
>
<
tr
>
<
td
id
=
"tdtop"
><
label
for
=
"forma"
>Modules
de formation
:</
label
> </
td
>
<
td
>
<
select
name
=
"forma"
id
=
"forma"
multiple
>
<
option
value
=
"HTML5"
>HTML5</
option
>
<
option
value
=
"CSS3"
>CSS3</
option
>
<
option
value
=
"JavaScript"
>JavaScript</
option
>
<
option
value
=
"JQuery"
>JQuery</
option
>
</
select
>
</
td
>
</
tr
>
<
tr
>
<
td
colspan
=
"2"
>
(*) Champs obligatoires <
br
>
<
input
type
=
"submit"
value
=
"Envoyer"
>
<
input
type
=
"reset"
value
=
"Annuler"
>
</
td
>
</
tr
>
</
table
>
</
form
>
</
fieldset
>
<
script
>
form
=
document.
getElementById
(
"myform"
);
form.
addEventListener
(
'submit'
,
function
(
e
){
verif
();
e
.
preventDefault
();})
</
script
>
<
script
src
=
"ex1.js"
></
script
>
</
body
>
</
html
>
body
{
background-color
:
#BDB76B
;
}
legend
{
color
:
darkgreen
;
}
fieldset
{
width
:
60
%
;
border
:
2
px
darkgreen
solid
;
border-radius
:
10
px
;
}
#tdtop
{
vertical-align
:
top
;
}
function
verif
(){
if
(name
==
""
||
cin
==
""
||
mail
==
""
){
alert
(
"nom, cin et adresse email doivent etre
non vides"
);
return
false
;
}
//vérification Maj
const
myArray
=
name.
split
(
" "
);
//verif prenom
let
p
=
myArray[
0
];
let
testp
=
p.
toUpperCase
();
if
(p
!=
testp){
alert
(
"Prénom n'est pas en majus)"
);
return
false
;
}
myArray.
forEach
(
element
=>
{
if
(element
[
0
]
>=
"a"
&&
element
[
0
]
<=
"z"
){
alert
(
"Prénom et nom sont invalides (Min
au debut)"
);
return
false
;
}
});
//verif cin
if
(cin.length
!=
8
||
isNaN
(cin)){
alert
(
"num CIN est invalide"
);
return
false
;
}
//verif mail
if
(mail.
indexOf
(
"@"
)
<
0
||
mail.
indexOf
(
"."
)
==-
1
){
alert
(
"adresse email est invalide"
);
return
false
;
}
if
(count
>
2
){
alert
(
"Nombre des formations max est 2"
);
return
false
;
}
//formulaire valide
alert
(
"Formulaire Valide !"
);
}
Exercice 3
<!
DOCTYPE
html
>
<
html
lang
=
"en"
>
<
head
>
<
meta
charset
=
"UTF-8"
>
<
meta
http-equiv
=
"X-UA-Compatible"
content
=
"IE=edge"
>
<
meta
name
=
"viewport"
content
=
"width=device-width,
initial-scale=1.0"
>
<
title
>Document</
title
>
<
link
rel
=
"stylesheet"
href
=
"ex3.css"
>
</
head
>
<
body
>
<
form
id
=
"myform"
>
<
fieldset
id
=
"field1"
>
<
legend
>Vos Coordonnées</
legend
>
<
div
class
=
"container"
>
<
div
class
=
"formelem"
>
<
label
for
=
"lname"
>Nom:</
label
>
<
input
class
=
"data"
type
=
"text"
id
=
"lname"
placeholder
=
"votre nom"
>
</
div
>
<
div
class
=
"formelem"
>
<
label
for
=
"fname"
>Prénom:</
label
>
<
input
class
=
"data"
type
=
"text"
id
=
"fname"
placeholder
=
"votre prenom"
>
</
div
>
<
div
class
=
"formelem"
>
<
label
for
=
"daten"
>Date naissance:</
label
>
<
input
class
=
"data"
type
=
"date"
id
=
"daten"
>
</
div
>
<
div
class
=
"formelem"
>
<
label
for
=
"email"
>eMail :</
label
>
<
input
class
=
"data"
type
=
"email"
id
=
"email"
placeholder
=
"votre email"
>
</
div
>
<
div
class
=
"formelem"
>
<
label
for
=
"site"
>Site :</
label
>
<
input
class
=
"data"
type
=
"text"
placeholder
=
"Votre site"
id
=
"site"
>
</
div
>
<
div
class
=
"formelem"
>
<
label
for
=
"code"
>Mot de passe
:</
label
>
<
input
class
=
"data"
type
=
"password"
placeholder
=
"Votre site"
id
=
"code"
>
</
div
>
<
div
class
=
"formelem"
>
<
label
for
=
"genre"
>Genre :</
label
>
<
input
type
=
"radio"
name
=
"genre"
>Homme
<
input
type
=
"radio"
name
=
"genre"
>Femme
</
div
>
<
div
class
=
"formelem"
>
<
label
for
=
"pays"
>Pays :</
label
>
<
select
class
=
"data"
name
=
"pays"
id
=
"pays"
>
<
option
value
=
""
selected
>Votre
pay</
option
>
<
optgroup
label
=
"Afrique"
>
<
option
value
=
"tn"
>Tun</
option
>
<
option
value
=
"alg"
>Alger</
option
>
</
optgroup
>
<
optgroup
label
=
"Europe"
>
<
option
value
=
"fr"
>Fr</
option
>
<
option
value
=
"belg"
>Belg</
option
>
</
optgroup
>
<
optgroup
label
=
"Amérique"
>
<
option
value
=
"ca"
>Ca</
option
>
<
option
value
=
"arg"
>Arg</
option
>
</
optgroup
>
<
optgroup
label
=
"Autres"
>
<
option
value
=
"eur"
>Europ</
option
>
<
option
value
=
"asi"
>Asie</
option
>
</
optgroup
>
</
select
>
</
div
>
</
div
>
</
fieldset
>
<
fieldset
>
<
legend
>Vos formation</
legend
>
<
div
>
<
input
type
=
"checkbox"
name
=
"forma"
value
=
"html"
>
formation en html 5 <
br
>
<
input
type
=
"checkbox"
name
=
"forma"
value
=
"css"
>
formation en CSS3 <
br
>
<
input
type
=
"checkbox"
name
=
"forma"
value
=
"js"
>
formation en JS <
br
>
<
textarea
name
=
"desc"
id
=
"desc"
cols
=
"50"
rows
=
"5"
placeholder
=
"decrire votre besoin"
></
textarea
>
</
div
>
</
fieldset
>
<
fieldset
>
<
legend
>Envoyer nous votre photo</
legend
>
<
input
id
=
"myfile"
type
=
"file"
value
=
"Choisir
un fichier"
>
<
input
type
=
"hidden"
value
=
"taille max
10ko"
>
<
br
>
<
input
type
=
"reset"
value
=
"Effacer"
class
=
"btn"
>
<
input
type
=
"submit"
value
=
"Envoyer"
class
=
"btn"
>
</
fieldset
>
</
form
>
<
script
>
form
=
document.
getElementById
(
"myform"
);
form.
addEventListener
(
'submit'
,
function
(
e
){
e
.
preventDefault
();
verif
();})
</
script
>
<
script
src
=
"ex3.js"
></
script
>
</
body
>
</
html
>
fieldset
{
margin
:
10
px
;
width
:
50
%
;
box-shadow
:
-1
px
-1
px
10
px
2px
rgb
(
194
,
194
,
194
);
border
:
2
px
solid
rgb
(
194
,
194
,
194
);
border-radius
:
10
px
;
}
legend
{
padding
:
5px
;
border
:
4
px
solid
rgb
(
194
,
194
,
194
);
background-color
:
gainsboro
;
border-radius
:
7
px
;
font-weight
:
bold
;
font-size
:
large
;
}
label
{
/* Pour être sûrs que toutes les étiquettes
ont même taille et sont correctement alignées */
display
:
inline-block
;
width
:
80
px
;
font-weight
:
bold
;
}
#daten
{
vertical-align
:
top
;
}
#pays
{
width
:
81
%
;
height
:
37
px
;
}
.data
{
width
:
80
%
;
height
:
30
px
;
border
:
2
px
rgb
(
183
,
183
,
183
)
solid
;
border-radius
:
5
px
;
}
.green
{
border
:
4
px
solid
green
;
width
:
80
%
;
height
:
30
px
;
border-radius
:
5
px
;
}
textarea
{
border
:
2
px
rgb
(
183
,
183
,
183
)
solid
;
border-radius
:
5
px
;
width
:
90
%
;
}
.formelem
{
margin
:
10
px
;
}
.btn
{
font-weight
:
bold
;
margin
:
10
px
;
border-radius
:
15
px
;
border
:
none
;
padding
:
20
px
;
/*padding-left: 20px;
padding-right: 20px;*/
padding-top
:
10
px
;
padding-bottom
:
10
px
;
color
:
white
;
background-color
:
rgb
(
113
,
186
,
218
);
}
#myfile
{
margin-left
:
30
px
;
}
function
verif
(){
//collect data from form
lname
=
document.
getElementById
(
"lname"
).value;
fname
=
document.
getElementById
(
"fname"
).value;
daten
=
document.
getElementById
(
"daten"
).value;
site
=
document.
getElementById
(
"site"
).value;
code
=
document.
getElementById
(
"code"
).value;
mail
=
document.
getElementById
(
"email"
).value;
//required fields
if
(lname
==
""
){
alert
(
"le nom est obligatoire"
);
return
false
;
}
if
(fname
==
""
){
alert
(
"le prenom est obligatoire"
);
return
false
;
}
if
(daten
==
""
){
alert
(
"date naissance est obligatoire"
);
return
false
;
}
//Le mot de passe doit être formé exactement de
8 caractères.
if
(code.length
!=
8
){
alert
(
"pass invalide => 8 caracteres"
);
return
false
;
}
//Le choix d’un genre est obligatoire
genre
=
document.
getElementsByName
(
"genre"
);
if
(
!
genre[
0
].checked
&&
!
genre[
1
].checked){
alert
(
"genre obligatoire !"
);
return
false
;
}
//La sélection d’un pays est obligatoire
pays
=
document.
getElementById
(
"pays"
);
if
(pays.selectedIndex
<
1
){
alert
(
"pays obligatoire !"
);
return
false
;
}
// Au moins une formation est choisie
forma
=
document.
getElementsByName
(
"forma"
);
if
(
!
forma[
0
].checked
&&
!
forma[
1
].checked
&&
!
forma[
2
].checked){
alert
(
"formation obligatoire (min : 1)!"
);
return
false
;
}
//...........................
return
true
;
}
function
myfocus
(
i
d
) {
//recherche
input
=
document.
getElementById
(
id
);
input.
setAttribute
(
"class"
,
"data green"
);
}
Exercice 2
<!
DOCTYPE
html
>
<
html
lang
=
"en"
>
<
head
>
<
meta
charset
=
"UTF-8"
>
<
meta
http-equiv
=
"X-UA-Compatible"
content
=
"IE=edge"
>
<
meta
name
=
"viewport"
content
=
"width=device-width,
initial-scale=1.0"
>
<
title
>Document</
title
>
<
link
rel
=
"stylesheet"
href
=
"ex2.css"
>
</
head
>
<
body
>
<
form
action
=
""
id
=
"myform"
>
<
div
class
=
"container"
>
<
div
class
=
"cont2"
>
<
div
class
=
"elem"
>
<
label
for
=
"
dim"
>Taille</
label
>
<
input
onblur
=
"
fnblur
(
this
.
id
)"
onfocus
=
"
fnfocus
(
t
his
.
id
)"
class
=
"data"
type
=
"text"
name
=
"dim"
id
=
"dim"
>
</
div
>
<
div
class
=
"elem"
>
<
label
for
=
"
min"
>Borne inf</
label
>
<
input
onblur
=
"
fnblur
(
this
.
id
)"
onfocus
=
"
fnfocus
(
t
his
.
id
)"
class
=
"data"
type
=
"text"
name
=
"min"
id
=
"min"
>
</
div
>
<
div
class
=
"elem"
>
<
label
for
=
"
max"
>Borne sup</
label
>
<
input
onblur
=
"
fnblur
(
this
.
id
)"
onfocus
=
"
fnfocus
(
t
his
.
id
)"
class
=
"data"
type
=
"text"
name
=
"max"
id
=
"max"
>
</
div
>
</
div
>
<
div
class
=
"cont3"
>
<
input
type
=
"submit"
value
=
"generate"
name
=
"btn"
id
=
"btn"
class
=
"mybtn"
>
</
div
>
<
div
class
=
"result"
>
<
table
id
=
"mytable" class="table-light"
></
table
>
</
div
>
</
div
>
</
form
>
<
script
>
form
=
document.
getElementById
(
"myform"
);
form.
addEventListener
(
'submit'
,
function
(e
)
{
e
.
p
reventDefault
();
verif
(); })
</
script
>
<
script
src
=
"ex2.js"
></
script
>
</
body
>
</
html
>
.container
{
width
:
70
%
;
margin
:
auto
;
}
label
{
padding-left
:
10
px
;
}
.cont2
{
margin
:
auto
;
display
:
flex
;
justify-content
:
space-around
;
}
.data
{
margin
:
10
px
;
height
:
30
px
;
border-radius
:
5
px
;
font-size
:
15
px
;
border
:
1px
solid
#299be4
;
}
.focusClass
{
outline
:
none
;
box-shadow
:
-1
px
-1
px
10
px
2px
#299be4
;
}
.elem
{
display
:
grid
;
}
.cont3
{
display
:
flex
;
justify-content
:
space-around
;
}
.result
{
display
:
flex
;
justify-content
:
space-around
;
}
.mybtn
{
margin-bottom
:
30
px
;
padding
:
10
px
;
border
:
none
;
border-radius
:
7
px
;
margin-top
:
30
px
;
width
:
80
%
;
color
:
white
;
background-color
:
#299be4
;
font-size
:
larger
;
font-weight
:
bold
;
}
table
{
width
:
80
%
;
border-collapse
:
collapse
;
border
:
2
px
solid
#ddd
;
text-align
:
center
;
}
.table-light
td
{
padding
:
10
px
;
border-right
:
2px
solid
#ddd
;
}
.table-light
tr
{
border-top
:
2
px
solid
#ddd
;
}
.table-light
tr
:nth-child
(
odd
){
background-color
:
rgba
(
0
,
0
,
0
,
0.05
);
}
function
verif
(
params
) {
}
function
fnfocus
(
i
d
) {
//recherche
console.
log
(
"focus fn on => "
+
id
);
input
=
document.
getElementById
(
id
);
input.
setAttribute
(
"class"
,
"focusClass data"
);
}
function
fnblur
(
id
) {
//recherche
console.
log
(
"blur fn on => "
+
id
);
input
=
document.
getElementById
(
id
);
input.
setAttribute
(
"class"
,
"data"
);
}
function
verif
() {
//test taille vide/entier
let
dim
=
document.
getElementById
(
"dim"
).value;
if
(dim
==
""
||
isNaN
(dim)){
alert
(
"dim requried or must be number"
);
return
false
;
}
//test max/min
let
max
=
document.
getElementById
(
"max"
).value;
let
min
=
document.
getElementById
(
"min"
).value;
if
(min
==
""
||
max
==
""
||
isNaN
(min)
||
isNaN
(max)){
alert
(
"min & max requried"
);
return
false
;
}
else
{
if
(
parseInt
(min)
>
parseInt
(max)){
alert
(
"min > max : error"
);
return
false
;
}
}
//5.3 --> 6
table
=
document.
getElementById
(
"mytable"
);
//recherche
table
//calcule "ROWS"
rows
=
Math.
ceil
(
parseInt
(dim)
/
10
);
// nb des lignes
for
(
let
j
=
0
;j
<
rows;j
++
){
tr
=
document.
createElement
(
"tr"
);
//creation
TR
for
(
let
i
=
0; i
<
10
; i
++
) {
//...........................
td
=
document.
createElement
(
"td"
);
//creation
td
let
n
=
getRandomInt
(min,max);
//création
random int
tn
=
document.
createTextNode
(n);
//création
node text
//-------------------------------
td.
appendChild
(tn);
tr.
appendChild
(td);
}
table.
appendChild
(tr);
}
return
true
;
}
function
fnfocus
(
i
d
) {
//recherche
console.
log
(
"focus fn on => "
+
id
);
input
=
document.
getElementById
(
id
);
input.
setAttribute
(
"class"
,
"focusClass data"
);
}
function
fnblur
(
id
) {
//recherche
console.
log
(
"blur fn on => "
+
id
);
input
=
document.
getElementById
(
id
);
input.
setAttribute
(
"class"
,
"data"
);
}
function
getRandomInt
(
min
,
max
) {
min
=
Math.
ceil
(
Number
(
min
));
max
=
Math.
floor
(
Number
(
max
)
+
1
);
return
Math.
floor
(Math.
random
()
*
(max
-
min
))
+
min
;
}
Exercice 4 :
<!
doctype
html
>
<
html
>
<
head
>
<
title
>Manipuler Styles</
title
>
<
meta
charset
=
"utf-8"
>
</
head
>
<
body
>
<
div
>
<
table
border
=
"1"
>
<
tr
>
<
td
>couleur de fond (rgb)</
td
>
<
td
>
0<
input
type
=
"range"
min
=
"
0"
max
=
"255"
step
=
5
name
=
'color'
oninput
=
'
update
(
this
)'
value
=
"0"
>255
</
td
>
<
td
rowspan
=
"6"
width
=
"400"
align
=
"center"
>
<
h1
id
=
"p1"
>Titre de test</
h1
>
</
td
>
</
tr
>
<
tr
>
<
td
>padding</
td
>
<
td
>
0<
input
type
=
"range"
name
=
"padding"
min
=
"0"
max
=
"
30"
step
=
5
oninput
=
'
update
(
this
)'
value
=
"0"
>30
</
td
>
</
tr
>
<
tr
>
<
td
>height</
td
>
<
td
>
0<
input
type
=
"range"
min
=
"
5"
max
=
"200"
name
=
"height"
step
=
50
value
=
"0"
oninput
=
'
update
(
this
)'
>200
</
td
>
</
tr
>
<
tr
>
<
td
>width</
td
>
<
td
>
0<
input
type
=
"range"
min
=
"
5"
max
=
"200"
name
=
"width"
step
=
50
value
=
"0"
oninput
=
'
update
(
this
)'
>200
</
td
>
</
tr
>
<
tr
>
<
td
>arrondi</
td
>
<
td
>
0<
input
type
=
"range"
min
=
"
5"
max
=
"30"
name
=
"radius"
step
=
5
value
=
"0"
oninput
=
'
update
(
this
)'
>30
</
td
>
</
tr
>
<
tr
>
<
td
>rotation</
td
>
<
td
>
0<
input
type
=
"range"
min
=
"
5"
max
=
"180"
name
=
"rotation"
step
=
5
value
=
"0"
oninput
=
'
update
(
this
)'
>180
</
td
>
</
tr
>
</
table
>
</
div
>
<
script
src
=
"ex4.js"
></
script
>
</
body
>
</
html
>
function
update
(
elem
) {
var
p1
=
document.
getElementById
(
"p1"
);
//recherche
if
(elem
.
getAttribute
(
"name"
)
==
"color"
) {
p1.
style
.background
=
"rgb("
+
elem
.value
+
","
+
elem
.value
+
","
+
elem
.value
+
")"
;
var
c
=
255
-
elem
.value;
p1.
style
.color
=
"rgb("
+
c
+
","
+
c
+
","
+
c
+
")"
;
}
else
if
(
elem
.
getAttribute
(
"name"
)
==
"padding"
)
{
p1.
style
.padding
=
elem
.value
+
"px"
;
}
else
if
(
elem
.
getAttribute
(
"name"
)
==
"height"
)
{
p1.
style
.height
=
elem
.value
+
"px"
;
}
else
if
(
elem
.
getAttribute
(
"name"
)
==
"width"
)
{
p1.
style
.width
=
elem
.value
+
"px"
;
}
else
if
(
elem
.
getAttribute
(
"name"
)
==
"radius"
)
{
p1.
style
.borderRadius
=
elem
.value
+
"px"
;
}
else
if
(
elem
.
getAttribute
(
"name"
)
==
"rotation"
)
{
p1.
style
.transform
=
"rotate("
+
elem
.value
+
"deg)"
;
}
}