Exemple
d'utilisation
<SCRIPT language=JavaScript type=text/javascript>
<!--
var re_date = /^\d{1,2}\/\d{1,2}\/\d{4}$/;
var re_date2 = /^\d{4}\/\d{1,2}\/\d{1,2}$/;
var re_inv = /^(\d{4})\/(\d{1,2})\/(\d{1,2})$/;
var re_email = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$/ ;
var msg = "";
var msg_debut = "Attention,\n";
var tempo = new Array();
var max_mois = new Array();
for (n=1;n<100;n++) {
if (n==4 || n==6 || n==9 || n==11) max_mois[n] = 30;
else max_mois[n] = 31;
}
//====================
function valide() {
//====================
with (document.test) {
// vérification de base 99/99/9999
t_date = re_date.test(date.value);
t_date2 = re_date2.test(date.value);
// vérification totale
if (t_date) t_date = t_date && analyse(date.value,1,2,3);
else if (t_date2) {
t_date = t_date2 && analyse(date.value,3,2,1);
if (t_date) date.value = inverse (date.value);
}
t_email = re_email.test(email.value);
}
if (t_email && t_date) msg = "test correct.";
else {
msg = msg_debut;
if (!t_email) msg += "l'adresse email est incorrecte\n" ;
if (!t_date) msg += "la date est incorrecte\n" ;
}
alert(msg);
}
//====================
function analyse(quoi,p_jour,p_mois,p_an) {
//====================
tempo = quoi.split('/')
jour = eval(tempo[(p_jour-1)]);
mois = eval(tempo[(p_mois-1)]);
an = eval(tempo[(p_an-1)]);
if (bisx(an)) max_mois[2] = 29;
else max_mois[2] = 28;
t_tempo = (jour<=max_mois[mois] && mois<=12 && mois>=1)
return t_tempo;
}
//====================
function bisx(quoi) {
//====================
if ((quoi % 100 == 0) && (quoi % 400 == 0)) return true;
else if ((quoi % 4) == 0) return true;
return false;
}
//====================
function inverse(quoi) {
//====================
return quoi.replace(re_inv, "$3/$2/$1");
}
//-->
</SCRIPT>
|