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

JavaScript Discussion :

Vérification format adresse mail dans formulaire


Sujet :

JavaScript

  1. #1
    Futur Membre du Club
    Profil pro
    Inscrit en
    Août 2013
    Messages
    17
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2013
    Messages : 17
    Points : 7
    Points
    7
    Par défaut Vérification format adresse mail dans formulaire
    Bonjour je travaille sur un formulaire en java , j'ai juste un dernier probleme , c'est pour verifier si l'adresse mail a un format correct
    Voici mon code en entier , en rouge la partie concerné :

    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
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Document sans titre</title>
    <style>
          body {
           padding-top: 50px;
          }
    
          .form_col {
            display: inline-block;
            margin-right: 15px;
            padding: 3px 0px;
            width: 200px;
            min-height: 1px;
            text-align: right;
          }
    
          input {
            padding: 2px;
            border: 1px solid #CCC;
            -moz-border-radius: 2px;
            -webkit-border-radius: 2px;
            border-radius: 2px;
          }
    
          input:focus {
            border-color: rgba(82, 168, 236, 0.75);
            -moz-box-shadow: 0 0 8px rgba(82, 168, 236, 0.5);
            -webkit-box-shadow: 0 0 8px rgba(82, 168, 236, 0.5);
            box-shadow: 0 0 8px rgba(82, 168, 236, 0.5);
          }
    
          .correct {
            border-color: rgba(68, 191, 68, 0.75);
          }
    
          .correct:focus {
            border-color: rgba(68, 191, 68, 0.75);
            -moz-box-shadow: 0 0 8px rgba(68, 191, 68, 0.5);
            -webkit-box-shadow: 0 0 8px rgba(68, 191, 68, 0.5);
            box-shadow: 0 0 8px rgba(68, 191, 68, 0.5);
          }
    
          .incorrect {
            border-color: rgba(191, 68, 68, 0.75);
          }
    
          .incorrect:focus {
            border-color: rgba(191, 68, 68, 0.75);
            -moz-box-shadow: 0 0 8px rgba(191, 68, 68, 0.5);
            -webkit-box-shadow: 0 0 8px rgba(191, 68, 68, 0.5);
            box-shadow: 0 0 8px rgba(191, 68, 68, 0.5);
          }
    
          .tooltip {
            display: inline-block;
            margin-left: 20px;
            padding: 2px 4px;
            border: 1px solid #555;
            background-color: #CCC;
            -moz-border-radius: 4px;
            -webkit-border-radius: 4px;
            border-radius: 4px;
          }
        </style>
    </head>
    
    <body>
    <form id="myForm">
    
          
    
          <label class="form_col" for="lastName">Nom :</label>
          <input name="lastName" id="lastName" type="text" />
          <span class="tooltip">Un nom ne peut pas faire moins de 2 caractères</span>
          <br /><br />
    
          <label class="form_col" for="firstName">Prénom :</label>
          <input name="firstName" id="firstName" type="text" />
    
          <span class="tooltip">Un prénom ne peut pas faire moins de 2 caractères</span>
          <br /><br />
    
         
    
          <label class="form_col" for="entreprise">Entreprise :</label>
          <input name="entreprise" id="entreprise" type="text" />
          <span class="tooltip">Une entreprise ne peut pas faire moins de 2 caractères</span>
          <br /><br />
          
          <label class="form_col" for="adresse">Adresse :</label>
          <input name="adresse" id="adresse" type="text" />
          <span class="tooltip">Une adresse ne peut pas faire moins de 5 caractères</span>
          <br /><br />
          
          <label class="form_col" for="CP">Code Postal :</label>
          <input name="CP" id="CP" type="text" />
          <span class="tooltip">Un code postal ne peut pas faire moins de 2 caractères</span>
          <br /><br />
          
          <label class="form_col" for="ville">Ville :</label>
          <input name="ville" id="ville" type="text" />
          <span class="tooltip">Une ville ne peut pas faire moins de 2 caractères</span>
          <br /><br />
          
          <label class="form_col" for="ntel1">N° de téléphone 1 :</label>
          <input name="ntel1" id="ntel1" type="text" />
          <span class="tooltip">Un n° de téléphone doit comporter 10 chiffres</span>
          <br /><br />
          
          <label class="form_col" for="ntel2">N° de téléphone 2 :</label>
          <input name="ntel2" id="ntel2" type="text" />
          <span class="tooltip">Un n° de téléphone doit comporter 10 chiffres</span>
          <br /><br />
          
          <label class="form_col" for="fax">Fax :</label>
          <input name="fax" id="fax" type="text" />
          <span class="tooltip">Un n° de fax doit comporter 10 chiffres</span>
          <br /><br />
          
          <label class="form_col" for="site">Site web :</label>
          <input name="site" id="site" type="text" />
          <span class="tooltip">Un site web ne peut pas faire moins de 4 caractères</span>
          <br /><br />
          
          <label class="form_col" for="mail">Mail :</label>
          <input name="mail" id="mail" type="text" />
          <span class="tooltip">Format d'adresse mail non valide</span>
          <br /><br />
    
    
    
    
    
          <label class="form_col" for="pwd1">Mot de passe :</label>
          <input name="pwd1" id="pwd1" type="password" />
          <span class="tooltip">Le mot de passe ne doit pas faire moins de 6 caractères</span>
    
          <br /><br />
    
          <label class="form_col" for="pwd2">Mot de passe (confirmation) :</label>
          <input name="pwd2" id="pwd2" type="password" />
          <span class="tooltip">Le mot de passe de confirmation doit être identique à celui d'origine</span>
          <br /><br />
    
          
    
          
    
          <span class="form_col"></span>
          <input type="submit" value="M'inscrire" /> <input type="reset" value="Réinitialiser le formulaire" />
    
        </form>
    
    
        <script>
    
            // Fonction de désactivation de l'affichage des "tooltips"
            
            function deactivateTooltips() {
            
                var spans = document.getElementsByTagName('span'),
                spansLength = spans.length;
                
                for (var i = 0 ; i < spansLength ; i++) {
                    if (spans[i].className == 'tooltip') {
                        spans[i].style.display = 'none';
                    }
                }
            
            }
    
    
            // La fonction ci-dessous permet de récupérer la "tooltip" qui correspond à notre input
    
            function getTooltip(elements) {
            
                while (elements = elements.nextSibling) {
                    if (elements.className === 'tooltip') {
                        return elements;
                    }
                }
                
                return false;
            
            }
    
    
            // Fonctions de vérification du formulaire, elles renvoient "true" si tout est ok
    
            var check = {}; // On met toutes nos fonctions dans un objet littéral
    
            
    
            check['lastName'] = function(id) {
    
                var name = document.getElementById(id),
                    tooltipStyle = getTooltip(name).style;
    
                if (name.value.length >= 2) {
                    name.className = 'correct';
                    tooltipStyle.display = 'none';
                    return true;
                } else {
                    name.className = 'incorrect';
                    tooltipStyle.display = 'inline-block';
                    return false;
                }
    
            };
    
            check['firstName'] = check['lastName']; // La fonction pour le prénom est la même que celle du nom
    
            
    
            check['entreprise'] = function() {
            
                var entreprise = document.getElementById('entreprise'),
                    tooltipStyle = getTooltip(entreprise).style;
                
                if (entreprise.value.length >= 2) {
                    entreprise.className = 'correct';
                    tooltipStyle.display = 'none';
                    return true;
                } else {
                    entreprise.className = 'incorrect';
                    tooltipStyle.display = 'inline-block';
                    return false;
                }
    
            };
    		
    		check['adresse'] = function() {
            
                var adresse = document.getElementById('adresse'),
                    tooltipStyle = getTooltip(adresse).style;
                
                if (adresse.value.length >= 2) {
                    adresse.className = 'correct';
                    tooltipStyle.display = 'none';
                    return true;
                } else {
                    adresse.className = 'incorrect';
                    tooltipStyle.display = 'inline-block';
                    return false;
                }
    
            };
    		
    		check['CP'] = function() {
            
                var CP = document.getElementById('CP'),
                    tooltipStyle = getTooltip(CP).style;
    				
                
                if (CP.value.length == 5) {
                    CP.className = 'correct';
                    tooltipStyle.display = 'none';
                    return true;
                } else {
                    CP.className = 'incorrect';
                    tooltipStyle.display = 'inline-block';
                    return false;
                }
    
            };
    		
    		check['ville'] = function() {
            
                var ville = document.getElementById('ville'),
                    tooltipStyle = getTooltip(ville).style;
                
                if (ville.value.length >= 2) {
                    ville.className = 'correct';
                    tooltipStyle.display = 'none';
                    return true;
                } else {
                    ville.className = 'incorrect';
                    tooltipStyle.display = 'inline-block';
                    return false;
                }
    
            };
    		
    		check['ntel1'] = function() {
            
                var ntel1 = document.getElementById('ntel1'),
                    tooltipStyle = getTooltip(ntel1).style;
                
                if (ntel1.value.length >= 2) {
                    ntel1.className = 'correct';
                    tooltipStyle.display = 'none';
                    return true;
                } else {
                    ntel1.className = 'incorrect';
                    tooltipStyle.display = 'inline-block';
                    return false;
                }
    
            };
    		
    		check['ntel2'] = function() {
            
                var ntel2 = document.getElementById('ntel2'),
                    tooltipStyle = getTooltip(ntel2).style;
                
                if (ntel2.value.length == 10) {
                    ntel2.className = 'correct';
                    tooltipStyle.display = 'none';
                    return true;
                } else {
                    ntel2.className = 'incorrect';
                    tooltipStyle.display = 'inline-block';
                    return false;
                }
    
            };
    		
    		check['fax'] = function() {
            
                var fax = document.getElementById('fax'),
                    tooltipStyle = getTooltip(fax).style;
                
                if (fax.value.length == 10) {
                    fax.className = 'correct';
                    tooltipStyle.display = 'none';
                    return true;
                } else {
                    fax.className = 'incorrect';
                    tooltipStyle.display = 'inline-block';
                    return false;
                }
    
            };
    		
    		check['site'] = function() {
            
                var site = document.getElementById('site'),
                    tooltipStyle = getTooltip(site).style;
                
                if (site.value.length >= 4) {
                    site.className = 'correct';
                    tooltipStyle.display = 'none';
                    return true;
                } else {
                    site.className = 'incorrect';
                    tooltipStyle.display = 'inline-block';
                    return false;
                }
    
            };
    		
    		check['mail'] = function() {
            
                var mail = document.getElementById('mail'),
                    tooltipStyle = getTooltip(mail).style;
                
                if (mail.value.length >= 4) {
                    mail.className = 'correct';
                    tooltipStyle.display = 'none';
                    return true;
                } else {
                    mail.className = 'incorrect';
                    tooltipStyle.display = 'inline-block';
                    return false;
                }
    
            };
    
            check['pwd1'] = function() {
            
                var pwd1 = document.getElementById('pwd1'),
                    tooltipStyle = getTooltip(pwd1).style;
                
                if (pwd1.value.length >= 6) {
                    pwd1.className = 'correct';
                    tooltipStyle.display = 'none';
                    return true;
                } else {
                    pwd1.className = 'incorrect';
                    tooltipStyle.display = 'inline-block';
                    return false;
                }
            
            };
    
            check['pwd2'] = function() {
            
                var pwd1 = document.getElementById('pwd1'),
                    pwd2 = document.getElementById('pwd2'),
                    tooltipStyle = getTooltip(pwd2).style;
                
                if (pwd1.value == pwd2.value && pwd2.value != '') {
                    pwd2.className = 'correct';
                    tooltipStyle.display = 'none';
                    return true;
                } else {
                    pwd2.className = 'incorrect';
                    tooltipStyle.display = 'inline-block';
                    return false;
                }
            
            };
    
            
    
            // Mise en place des événements
            
            (function() { // Utilisation d'une fonction anonyme pour éviter les variables globales.
            
                var myForm = document.getElementById('myForm'),
                    inputs = document.getElementsByTagName('input'),
                    inputsLength = inputs.length;
            
                for (var i = 0 ; i < inputsLength ; i++) {
                    if (inputs[i].type == 'text' || inputs[i].type == 'password') {
            
                        inputs[i].onkeyup = function() {
                            check[this.id](this.id); // "this" représente l'input actuellement modifié
                        };
            
                    }
                }
            
                myForm.onsubmit = function() {
            
                    var result = true;
            
                    for (var i in check) {
                        result = check[i](i) && result;
                    }
            
                    if (result) {
                        alert('Le formulaire est bien rempli.');
                    }
            
                    return false;
            
                };
            
                myForm.onreset = function() {
            
                    for (var i = 0 ; i < inputsLength ; i++) {
                        if (inputs[i].type == 'text' || inputs[i].type == 'password') {
                            inputs[i].className = '';
                        }
                    }
            
                    deactivateTooltips();
            
                };
            
            })();
    
    
            // Maintenant que tout est initialisé, on peut désactiver les "tooltips"
            
            deactivateTooltips();
    
        </script>
    
    
    </body>
    </html>

  2. #2
    Membre à l'essai
    Profil pro
    Inscrit en
    Novembre 2011
    Messages
    32
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Novembre 2011
    Messages : 32
    Points : 20
    Points
    20
    Par défaut
    Bonjour,

    Pour faire une vérification si une adresse email a un format corect il faut passer par une expression régulière de type :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    ^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$

Discussions similaires

  1. Envoyer formulaire à une adresse mail dans l'anonymat
    Par face_connector dans le forum Balisage (X)HTML et validation W3C
    Réponses: 3
    Dernier message: 12/08/2012, 00h55
  2. Vérification d'adresse mail dans un formulaire
    Par kykyboogy dans le forum EDI, CMS, Outils, Scripts et API
    Réponses: 2
    Dernier message: 15/01/2009, 18h07
  3. vérification d'une adresse mail dans un formulaire via une regexp
    Par mattstriker dans le forum Général JavaScript
    Réponses: 5
    Dernier message: 11/04/2008, 19h12
  4. Vérification e-mail dans formulaire
    Par pp51 dans le forum Langage
    Réponses: 8
    Dernier message: 21/01/2007, 21h42
  5. Insertion d'adresse mail dans formulaire MS Access
    Par Gabe74 dans le forum Access
    Réponses: 1
    Dernier message: 06/09/2006, 13h49

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