Bonjour,

Dans mon formulaire, j'ai deux champs qui sont le nom et la date de naissance.

En gros, si la date de naissance est inférieur à 1900, alors un message d'erreur doit être affiché. Depuis que j'ai tenté de faire ce genre d'algo, plus rien ne fonctionne.

Je ne comprends pas ce qui bug ?



Code HTML : Sélectionner tout - Visualiser dans une fenêtre à part
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
<!DOCTYPE html>
<html lang="fr">
<head>
  <meta charset="utf-8">
  <title>Titre de la page</title>
  <link rel="stylesheet" href="style.css">
  <script src="script.js"></script>
</head>
<body>
<form action="#" onsubmit="return validation()" >
<br>
<label>Name : </label>
<br>
<input type="text" name="name" id="name">
<br>
<span id="nameError"></span>
<br>
<label>Date of birth : </label>
<br>
<input type="date" name="dateofbirth" id="dateofbirth">
<br>
<span id="dateofbirthError"></span>
<br>
<input type="submit" value="ok">
</form>
</body>
</html>

JS


Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
function validation() {
 
    var name = document.getElementById('name').value;
    var dateofbirth = document.getElementById('dateofbirth').value;
 
    if(name == ""){
        document.getElementById('nameError').innerHTML = " ** the name field is empty ! ";
         return false;
    }
 
    if(name.length < 2){
        document.getElementById('nameError').innerHTML = " ** min 2 characters ! ";
        return false;
    }
 
    if(!isValidDate(dateofbirth)){
        document.getElementById('dateofbirthError').innerHTML = " ** the date field is empty ! ";
        return false;
    }
}
 
 
 
function isValidDate(str) {
 
    var getvalue = str.split('-');
    var day = getvalue[2];
    var month = getvalue[1];
    var year = getvalue[0];
 
    if(year <= 1900){
        return false;
    }
    if (month < 1 || month > 12) { 
      return false;
     }
     if (day < 1 &|| day > 31) {
      return false;
     }
     if ((month==4 || month==6 || month==9 || month==11) && day==31) {
      return false;
     }
     if (month == 2) { 
      var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
      if (day>29 || (day==29 && !isleap)) {
       return false;
        }
     }
     else{
        return true;
 
     }
}