Script de vérification de formulaire
Bonjour, débutant en javascript, je voudrais tester chaque champs de mon formulaire, les consigner dans un tableau et si la longueur de mon tableau a changé, alors aucun Envoie sinon Envoie du formulaire. Le script que je vous soumet ne réagis pas à mon click.
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
|
function test()
{
/*tableau récuperation erreur*/
erreur=new Array();
/*nom*/
nom=document.forms["fContact"].fnom.value;
if(nom=="")
{
verif_nom="le Nom";
erreur.push(verif_nom);
}
/*prenom*/
prenom=document.forms["fContact"].fprenom.value;
if(prenom=="")
{
verif_nom="le Prénom";
erreur.push(verif_prenom);
}
/*email*/
var mail = new RegExp ( "^\\w[\\w+\.\-]*@[\\w\-]+\.\\w[\\w+\.\-]*\\w$", "gi" ) ;
adresse = document.forms["fContact"].fmail.value;
if (adresse.search(mail)== -1)
{
verif_email="email invalide";
erreur.push(verif_email);
}
/*téléphone*/
tel=document.forms["fContact"].fphone.value;
if (tel!=""){ if(isNaN(tel))
{
verif_tel_fixe="n° de téléphone érroné";
alert(verif_tel_fixe);
erreur.push(verif_tel_fixe);
}
else
{
vide="téléphone";
erreur.push(vide);
}
/*commentaires*/
com=document.forms["fContact"].fcoms.value;
if((com=="")||(com=="Commentaires")){
verif_com="Commentaires";
erreur.push(verif_com);
}
//récuperation des erreurs dans le tableau et alert pour l'utilisateur
if (erreur.length>0)
{
alert(erreur.join("\n"));
}
else
{
document.forms["fContact"].submit();
}
} |
je joins le body afin que vous puissiez tester.
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
|
<body>
<p align=center class="titre">ASSURANCE AUTO</p>
<div class="news" align=center>
<table width="900" height="80%" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td><table width="575" height="550" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td align=center> <img src="/images/Logo_Phenix.jpg" height="120" width="150"></img>
<!--Début de formulaire-->
<form name="fContact" method="post" enctype="text/plain" action="mailto:xxxxxxxxx@yahoo.fr">
<table width="540" border="0" cellspacing="0" cellpadding="0" align="center" class="medium">
<tr>
<td>Nom : </td>
<td> <input type="text" name="fnom" size="20" maxlength="19"></td>
</tr>
<tr>
<td> Prénom : </td>
<td><input type="text" name="fprenom" size="20" maxlength="19"></td>
</tr>
<tr>
<td>Téléphone : </td>
<td><input name="fphone" type="text" size="10" maxlength="10"></td>
</tr>
<tr>
<td> Email : </td>
<td><input type ="text" maxlength="30" size = "35"name="fmail"></td>
</tr>
<tr>
<td>Commentaires : </td>
<td><textarea name="fcom" cols="35" rows="5"></textarea>
</td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td></td>
<td><input type="button" value="Envoyer" onClick="test()"/> <input type="reset" value="Annuler"/> </td>
</tr>
</table>
</form>
</td>
</tr>
</table>
</td>
</tr>
</table>
<BR>
<p>
<p>
<p>
<p>
<p>
<p align=center><A HREF="assureAuto.html">Retour à l'index</A></p>
</div>
</body> |
PS: si vous avez des expressions régulières pour la vérification de nom et prénom, je suis prenneur.
Meric pour votre aide