Forum de discussion
Forum « Programmation Javascript » (archives)
Re: Probleme dans mon code javascript
Envoyé: 25 septembre 2003, 14h24 par Oznog
Ton code fonctionne à merveille. J'ai fait plusieurs tests avec succès sur les navigateurs et plate-formes suivantes (http://www.trucsweb.com/Javascript/val_date.htm) :
- MAC OS X
IE 5.2
Camino 0.7
Netscape 7.1
Mozilla 1.5a
Safari 1.0 (v85)
02/3/03
- MAC OS9
IE 5.01
Netscape® Communicator 4.5
- XP
IE 6
Netscape 7
Mozilla 1
Opera 6
NEtscape 4.7
J'ai refait le code, il y avait de drôle de caractères, c'était peut-être seulement ça.
Alors voilà le code :
<html>
<body>
<script>
function isDate(objname, DateField,msg) {
if(DateField.charAt(1) == '/') DateField = '0' + DateField
if(DateField.charAt(4) == '/') DateField = DateField.substring(0,3) + '0' + DateField.substring(3,9)
var ValidDigits = "0123456789/";
var allNum = "";
var InvalidDigit = "";
var mmddccyy = DateField.replace(/ /gi,"");
var month = mmddccyy.substring(0,2);
var day = mmddccyy.substring(3,5);
var year = mmddccyy.substring(6,10);
var today = new Date();
month = ((!month) ? today.getMonth():month-1);
var test = new Date(year,month,day);
for (i = 0; i < DateField.length; i++) {
ch = DateField.charAt(i);
for (j = 0; j < ValidDigits.length; j++) {
if (ch == ValidDigits.charAt(j)) {
break;
}
}
if (j == ValidDigits.length) {
InvalidDigit = 'YES';
break;
}
}
if (DateField.length == 8 && DateField / DateField == 1) {
mmddccyy = DateField.substring(0,2);
mmddccyy += '/';
mmddccyy += DateField.substring(2,4);
mmddccyy += '/';
mmddccyy += DateField.substring(4,8);
mmddccyy = mmddccyy.replace(/ /gi,"");
month = mmddccyy.substring(0,2);
day = mmddccyy.substring(3,5);
year = mmddccyy.substring(6,10);
month = ((!month) ? today.getMonth():month-1);
var test = new Date(year,month,day);
}
if (mmddccyy.length == 0) {
return mmddccyy;
} else if (InvalidDigit == 'YES') {
alert("Invalid "+msg+" : " + DateField + "\nPlease Enter Valid Date : Format MM/DD/CCYY");
objname.focus();
return "";
} else if (mmddccyy / mmddccyy == 1 || year.length != 4) {
alert("Invalid "+msg+" : " + DateField + "\nPlease Enter Valid Date : Format MM/DD/CCYY");
objname.focus();
return "";
} else if ( (month == test.getMonth()) && (day == test.getDate()) ) {
return mmddccyy;
} else {
alert("Invalid "+msg+" : " + DateField + "\nPlease Enter Valid Date : Format MM/DD/YYYY");
objname.focus();
return "";
}
}
</script>
<form name="monForm" method="post" action="">
<input type="text" name="maDate" onblur="isDate(this.form.maDate,this.value,'twErreur')" >
</form>
</body>
</html>
Ciao
Oznog
Réponses
|