IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

jQuery Discussion :

Confirmation après submit d'un formulaire


Sujet :

jQuery

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Inscrit en
    Août 2009
    Messages
    45
    Détails du profil
    Informations forums :
    Inscription : Août 2009
    Messages : 45
    Par défaut Confirmation après submit d'un formulaire
    Bonjour,

    J'ai utilisé ce bout de js pour la validation des input de mon formulaire:

    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
    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
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
     
    $(function() {
     
    	$("#newsletter").submit(function() {
     
    					 valid = true;
     
     
    					 if($("#nom").val()=="") {
     
    						 $("#nom").next(".error").fadeIn(1000).text("Nom requis");
    						 $("#nom").css("border-color","#f55");
    						 $("#nom").css("background-color","#fcc");
     
    						 valid = false;
    					 }
     
    					 else {
     
    						$("#nom").next(".error").slideUp();
    						$("#nom").css("border-color","#333");
    						$("#nom").css("background-color","#FFF");
     
    					 }
     
    					 if($("#prenom").val()=="") {
     
    						 $("#prenom").next(".error").fadeIn(1000).text("Prénom requis");
    						 $("#prenom").css("border-color","#f55");
    						 $("#prenom").css("background-color","#fcc");
     
    						 valid = false;
    					 }
     
    					 else {
     
    						$("#prenom").next(".error").slideUp();
    						$("#prenom").css("border-color","#333");
    						$("#prenom").css("background-color","#FFF");
     
    					 }
     
    					  if($("#mail").val()=="") {
     
    						 $("#mail").next(".error").fadeIn(1000).text("Email requis");
    						 $("#mail").css("border-color","#f55");
    						 $("#mail").css("background-color","#fcc");
     
    						 valid = false;
    					 }
     
    					 else if (!$("#mail").val().match(/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/)) {
     
    						 $("#mail").next(".error").fadeIn(1000).text("Email invalide");
    						 $("#mail").css("border-color","#f55");
    						 $("#mail").css("background-color","#fcc");
     
    						 valid = false;
    						 }
     
    					 else {
     
    						$("#mail").next(".error").slideUp();
    						$("#mail").css("border-color","#333");
    						$("#mail").css("background-color","#FFF");
     
    					 }
     
     
    					 return valid;
     
     
    					 });
     
    $("#nom").blur(function() {
     
    					valid = true;
     
    					 if($("#nom").val()=="") {
     
    								 $("#nom").next(".error").fadeIn(1000).text("Nom requis");
    								 $("#nom").css("border-color","#f55");
    								 $("#nom").css("background-color","#fcc");
    								 valid = false;
    							 }
     
    							 else {
    								$("#nom").next(".error").slideUp();
    								$("#nom").css("border-color","#333");
    								$("#nom").css("background-color","#FFF");
    							 }
     
     
    					return valid;
     
    					});
     
    $("#prenom").blur(function() {
     
    					valid = true;
     
    					 if($("#prenom").val()=="") {
     
    								 $("#prenom").next(".error").fadeIn(1000).text("Prénom requis");
    								 $("#prenom").css("border-color","#f55");
    								 $("#prenom").css("background-color","#fcc");
     
    								 valid = false;
    							 }
     
    							 else {
     
    								$("#prenom").next(".error").slideUp();
    								$("#prenom").css("border-color","#333");
    								$("#prenom").css("background-color","#FFF");
    							 }
     
     
    					return valid;
     
    					});
     
    $("#mail").blur(function() {
     
    					valid = true;
     
    					 if($("#mail").val()=="") {
     
    								 $("#mail").next(".error").fadeIn(1000).text("Email requis");
    								 $("#mail").css("border-color","#f55");
    								 $("#mail").css("background-color","#fcc");
     
    								 valid = false;
    							 }
     
    							 else if (!$("#mail").val().match(/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/)) {
    								 $("#mail").next(".error").fadeIn(1000).text("Email invalide");
    								 $("#mail").css("border-color","#f55");
    								 $("#mail").css("background-color","#fcc");
     
    								 valid = false;
    								 }
     
    							 else {
     
    								$("#mail").next(".error").slideUp();
    								$("#mail").css("border-color","#333");
    								$("#mail").css("background-color","#FFF");
     
    							 }
     
    							 return valid;
     
    					});
     
    	});
    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
     
    <form name="newsletter" id="newsletter" action="index.php" method="POST">
     
     
    <span class="ok"></span>
     
     
    <label for="nom">Nom :</label>
    <input type="text" name="nom" id="nom" /><span class="error"></span>
     
    <label for="prenom">Pr&eacute;nom :</label>
    <input type="text" name="prenom" id="prenom" /><span class="error"></span>
     
    <label for="mail">Email :</label>
    <input type="text" name="mail" id="mail" /><span class="error"></span>
     
    <input type="submit" id="submit" value="S'inscrire" />
     
     
    </form>
    Tout fonctionne à merveille ;-)

    Mais j'aimerai savoir comment je peux faire pour afficher un petit message dans un span en dessous du formulaire, par exemple, pour confirmer que le mail à bien été envoyé...

    D'avance merci beaucoup .

    Alex

  2. #2
    Rédacteur

    Avatar de danielhagnoul
    Homme Profil pro
    Étudiant perpétuel
    Inscrit en
    Février 2009
    Messages
    6 389
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 74
    Localisation : Belgique

    Informations professionnelles :
    Activité : Étudiant perpétuel
    Secteur : Enseignement

    Informations forums :
    Inscription : Février 2009
    Messages : 6 389
    Billets dans le blog
    125
    Par défaut
    Bonsoir.

    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
    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
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    <!doctype html>
    <html lang="fr">
    <head>
    	<meta charset="utf-8">
    	<meta name="Author" content="Daniel Hagnoul">
    	<meta name="description" content="Page type">
    	<title>Page type</title>
    	<style>
    		/* BASE */
    		body {
    			background-color:#ffffff;
    			color:#000000;
    			font-family:sans-serif;
    			font-size:medium;
    			font-style:normal;
    			font-weight:normal;
    			line-height:normal;
    			letter-spacing:normal;
    		}
    		h1,h2,h3,h4,h5 {
    			font-family:serif;
    		}
    		div,p,h1,h2,h3,h4,h5,h6,ul,ol,dl,form,table,img {
    			margin:0px;
    			padding:0px;
    		}
    		p {
    			padding:6px;
    		}
    		ul,ol,dl {
    			list-style:none;
    			padding-left:6px;
    			padding-top:6px;
    		}
    		li {
    			padding-bottom:6px;
    		}
     
    		/* TEST */
    	</style>
    	<script charset="utf-8" src="../lib/jqueryui/js/jquery-1.4.2.min.js"></script>
     	<script>
    		$(function(){
     
    			function verifNom(){
    				var valid = true;
    				var $nom = $("#nom");
     
    				if ($nom.val()=="") {
    					$nom.next(".error").fadeIn(1000).text("Nom requis");
    					$nom.css({
    						"border-color":"#f55",
    						"background-color":"#fcc"
    					});
    					valid = false;
    				} else {
    					$nom.next(".error").slideUp();
    					$nom.css({
    						"border-color":"#333",
    						"background-color":"#FFF"
    					});
    				}
     
    				return valid;
    			}
     
    			function verifPrenom(){
    				var valid = true;
    				var $prenom = $("#prenom");
     
    				if ($prenom.val()=="") {
    					$prenom.next(".error").fadeIn(1000).text("Prénom requis");
    					$prenom.css({
    						"border-color":"#f55",
    						"background-color":"#fcc"
    					});
    					valid = false;
    				} else {
    					$prenom.next(".error").slideUp();
    					$prenom.css({
    						"border-color":"#333",
    						"background-color":"#FFF"
    					});
    				}
     
    				return valid;
    			}
     
    			function verifMail(){
    				var valid = true;
    				var $mail = $("#mail");
     
    				if ($mail.val()=="") {
    					$mail.next(".error").fadeIn(1000).text("Email requis");
    					$mail.css({
    						"border-color":"#f55",
    						"background-color":"#fcc"
    					});
    					valid = false;
    				} else if (!$("#mail").val().match(/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/)) {
    					$mail.next(".error").fadeIn(1000).text("Email invalide");
    					$mail.css({
    						"border-color":"#f55",
    						"background-color":"#fcc"
    					});
    					valid = false;
    				} else {
    					$mail.next(".error").slideUp();
    					$mail.css({
    						"border-color":"#333",
    						"background-color":"#FFF"
    					});
    				}
     
    				return valid;
    			}
     
    			$("#newsletter").submit(function(){
    				var valid = verifNom();
     
    				valid = verifPrenom();
    				valid = verifMail();
     
    				if (valid){
    					$(this).next("span.ok").text("Le formulaire est valide, il a été envoyé !");
    				} else {
    					$(this).next("span.ok").text("Le formulaire n'est pas valide, il ne sera pas envoyé !");
    				}
     
    				return valid;
    			});
     
    			$("#nom").blur(function() {
    				verifNom();
    			});
     
    			$("#prenom").blur(function() {
    				verifPrenom();
    			});
     
    			$("#mail").blur(function() {
    				verifMail();
    			});
    		});
    	</script>
    </head>
    <body>
        <div id="conteneur">
     
    		<form id="newsletter" name="newsletter" action="index.php" method="POST">
    			<label for="nom">Nom :</label>
    			<input type="text" name="nom" id="nom" /> <span class="error"></span><br/>
    			<label for="prenom">Pr&eacute;nom :</label>
    			<input type="text" name="prenom" id="prenom" /> <span class="error"></span><br/>
    			<label for="mail">Email :</label>
    			<input type="text" name="mail" id="mail" /> <span class="error"></span><br/>
    			<input type="submit" id="submit" value="S'inscrire" />
    		</form>
    		<span class="ok"></span>
     
        </div> 
    </body>  
    </html>

    Blog

    Sans l'analyse et la conception, la programmation est l'art d'ajouter des bogues à un fichier texte vide.
    (Louis Srygley : Without requirements or design, programming is the art of adding bugs to an empty text file.)

  3. #3
    Membre averti
    Inscrit en
    Août 2009
    Messages
    45
    Détails du profil
    Informations forums :
    Inscription : Août 2009
    Messages : 45
    Par défaut
    Merci super j'ai bien pigé !!!

  4. #4
    Membre averti
    Inscrit en
    Août 2009
    Messages
    45
    Détails du profil
    Informations forums :
    Inscription : Août 2009
    Messages : 45
    Par défaut
    Ça marche top mais est-ce qu'il y aurait un moyen pour que le message reste visible quand le mail a été envoyé ?

    Parce que, là, lors du submit le message apparait 3 sec. et disparait...

    D'avance merci.

  5. #5
    Rédacteur

    Avatar de danielhagnoul
    Homme Profil pro
    Étudiant perpétuel
    Inscrit en
    Février 2009
    Messages
    6 389
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 74
    Localisation : Belgique

    Informations professionnelles :
    Activité : Étudiant perpétuel
    Secteur : Enseignement

    Informations forums :
    Inscription : Février 2009
    Messages : 6 389
    Billets dans le blog
    125
    Par défaut
    Bonjour.

    Ce comportement est normal. La soumission du formulaire provoque un rafraîchissement de la page web.

    Blog

    Sans l'analyse et la conception, la programmation est l'art d'ajouter des bogues à un fichier texte vide.
    (Louis Srygley : Without requirements or design, programming is the art of adding bugs to an empty text file.)

  6. #6
    Membre averti
    Inscrit en
    Août 2009
    Messages
    45
    Détails du profil
    Informations forums :
    Inscription : Août 2009
    Messages : 45
    Par défaut
    Oui ça c'est certain mais est-il possible de l'afficher autrement ? ou après le reloding de la page ou... je ne sais pas :-(

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. valeur des variables de formulaire après submit ?
    Par mounia.n dans le forum Langage
    Réponses: 4
    Dernier message: 28/05/2008, 16h25
  2. Réponses: 3
    Dernier message: 22/10/2007, 09h09
  3. Réponses: 4
    Dernier message: 22/05/2007, 10h52
  4. Réponses: 2
    Dernier message: 10/04/2007, 13h03
  5. Redirection vers la même page après submit de formulaire
    Par noOneIsInnocent dans le forum Struts 1
    Réponses: 5
    Dernier message: 27/10/2006, 15h36

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo