Tableau HTML avec Javascript
Bonjour à tous,
ja'i un tableau de 4 colonnes dans lequel se fait des calculs, le total de la colonne 2 est de 10000 et celui de la colonne 3 est de 9000. J'aimerais que, si un nombre est rentré et qu'il est supérieur à la limite imposé, qu'un message d'erreur s'affiche et que le nombre entré soit effacé. j'ai utilisé le bout de code ci-dessous pour pour cela, mais ça ne fonctionne pas :
Code:
1 2
|
document.form1.elements[champ.name].value = "" ; |
Voici mon code :
Code:
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
|
<html>
<head>
<script language="JavaScript">
<!--
function test_champ(champ) {
expr_reg = /^\d*$/ ;
// Ci-dessus : expression régulière qui match 0,1 ou plusieurs chiffres ;
// de cette façon on vérifie que l'utilisateur entre bien un nombre entier
if ( expr_reg.test(champ.value) ) {
// c'est bien un nombre entier
calcul_form() ;
} else {
// ce n'est pas un nombre entier
alert ("Veuillez entrer un nombre !") ;
document.form1.elements[champ.name].value = "" ; // on efface la valeur entrée erronée
calcul_form() ;
}
}
function calcul_form() {
// le with ci-dessous permet d'abréger "document.forms.form1.p1.value" en "p1.value"
with (document.forms.form1) {
total1.value = fraisdirects1.value *1 + fraisindirects1.value *1 ; // 1ère ligne
total2.value = fraisdirects2.value *1 + fraisindirects2.value *1 ; // 2ème ligne
total3.value = fraisdirects3.value *1 + fraisindirects3.value *1 ; // 3ème ligne
total4.value = fraisdirects4.value *1 + fraisindirects4.value *1 ; // 4ème ligne
total5.value = fraisdirects5.value *1 + fraisindirects5.value *1 ;
fraisdirects.value = fraisdirects1.value *1 + fraisdirects2.value *1 + fraisdirects3.value *1 + fraisdirects4.value *1 + fraisdirects5.value *1;
fraisindirects.value = fraisindirects1.value *1 + fraisindirects2.value *1 + fraisindirects3.value *1 + fraisindirects4.value *1 + fraisindirects5.value *1;
totalgeneral.value = total1.value *1 + total2.value *1 + total3.value *1 + total4.value *1 + total5.value *1;
if ( fraisdirects.value >= 10000 && fraisindirects >= 9000 ){
alert("Somme autoris\351e atteinte !");
document.form1.elements[champ.name].value = "" ;
}
}
}
// -->
</script>
</head>
<body>
<form name="form1" action="">
<table border="1" cellspacing="0" cellpadding="10">
<tr bgcolor="#CCCCCC">
<th>Année</th>
<th>Frais directs</th>
<th>Frais indirects</th>
<th>Total</th>
</tr>
<tr>
<th><input type="text" value="<?=date('Y');?>" readonly></th>
<th><input type="text" name="fraisdirects1" style="text-align:right;" onkeyup="test_champ(this)"/></th>
<th><input type="text" name="fraisindirects1" style="text-align:right;" onkeyup="test_champ(this)"/></th>
<th bgcolor="#CCCCCC"><input type="text" name="total1" style="text-align:right;" readonly /></th>
</tr>
<tr>
<th><input type="text" value="<?=date('Y')+1;?>" readonly></th>
<th><input type="text" name="fraisdirects2" style="text-align:right;" onkeyup="test_champ(this)"/></th>
<th><input type="text" name="fraisindirects2" style="text-align:right;" onkeyup="test_champ(this)"/></th>
<th bgcolor="#CCCCCC"><input type="text" name="total2" style="text-align:right;" readonly /></th>
</tr>
<tr>
<th><input type="text" value="<?=date('Y')+2;?>" readonly></th>
<th><input type="text" name="fraisdirects3" style="text-align:right;" onkeyup="test_champ(this)"/></th>
<th><input type="text" name="fraisindirects3" style="text-align:right;" onkeyup="test_champ(this)"/></th>
<th bgcolor="#CCCCCC"><input type="text" name="total3" style="text-align:right;" readonly /></th>
</tr>
<tr>
<th><input type="text" value="<?=date('Y')+3;?>" readonly></th>
<th><input type="text" name="fraisdirects4" style="text-align:right;" onkeyup="test_champ(this)"/></th>
<th><input type="text" name="fraisindirects4" style="text-align:right;" onkeyup="test_champ(this)"/></th>
<th bgcolor="#CCCCCC"><input type="text" name="total4" style="text-align:right;" readonly /></th>
</tr>
<tr>
<th><input type="text" value="<?=date('Y')+4;?>" readonly></th>
<th><input type="text" name="fraisdirects5" style="text-align:right;" onkeyup="test_champ(this)"/></th>
<th><input type="text" name="fraisindirects5" style="text-align:right;" onkeyup="test_champ(this)"/></th>
<th bgcolor="#CCCCCC"><input type="text" name="total5" style="text-align:right;" readonly /></th>
</tr>
<tr>
<td bgcolor="#CCCCCC"><b>Total général</b>
<th bgcolor="#CCCCCC"><input type="text" value="10000" style="text-align:right; font-weight: bold;" readonly />
<th bgcolor="#CCCCCC"><input type="text" value="9000" style="text-align:right; font-weight: bold;" readonly />
<th bgcolor="#CCCCCC"><input type="text" name="totalgeneral" style="text-align:right; font-weight: bold;" readonly />
</th>
</table>
</form>
</body>
</html> |
J'ai besoin de votre aide.
Merci