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 :

Vérification de champs dans formulaire


Sujet :

jQuery

  1. #1
    Membre à l'essai
    Profil pro
    Inscrit en
    Décembre 2013
    Messages
    30
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2013
    Messages : 30
    Points : 21
    Points
    21
    Par défaut Vérification de champs dans formulaire
    Bonjour a tous,
    Je suis en train de faire un formulaire a partir d'un récupéré sur le net,

    Sur le formulaire actuel, l’étape de validation vérifie tous les champs.
    Ce que j'aimerais c'est quel vérifie uniquement "pays, code postale et adresse" .
    Comment cela est-il possible ?
    Un grand merci a vous.

    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
    $(function() {
    	/*
    	number of fieldsets
    	*/
    	var fieldsetCount = $('#formElem').children().length;
     
    	/*
    	current position of fieldset / navigation link
    	*/
    	var current 	= 1;
     
    	/*
    	sum and save the widths of each one of the fieldsets
    	set the final sum as the total width of the steps element
    	*/
    	var stepsWidth	= 0;
        var widths 		= new Array();
    	$('#steps .step').each(function(i){
            var $step 		= $(this);
    		widths[i]  		= stepsWidth;
            stepsWidth	 	+= $step.width();
        });
    	$('#steps').width(stepsWidth);
     
    	/*
    	to avoid problems in IE, focus the first input of the form
    	*/
    	$('#formElem').children(':first').find(':input:first').focus();	
     
    	/*
    	show the navigation bar
    	*/
    	$('#navigation').show();
     
    	/*
    	when clicking on a navigation link 
    	the form slides to the corresponding fieldset
    	*/
        $('#navigation a').bind('click',function(e){
    		var $this	= $(this);
    		var prev	= current;
    		$this.closest('ul').find('li').removeClass('selected');
            $this.parent().addClass('selected');
    		/*
    		we store the position of the link
    		in the current variable	
    		*/
    		current = $this.parent().index() + 1;
    		/*
    		animate / slide to the next or to the corresponding
    		fieldset. The order of the links in the navigation
    		is the order of the fieldsets.
    		Also, after sliding, we trigger the focus on the first 
    		input element of the new fieldset
    		If we clicked on the last link (confirmation), then we validate
    		all the fieldsets, otherwise we validate the previous one
    		before the form slided
    		*/
            $('#steps').stop().animate({
                marginLeft: '-' + widths[current-1] + 'px'
            },500,function(){
    			if(current == fieldsetCount)
    				validateSteps();
    			else
    				validateStep(prev);
    			$('#formElem').children(':nth-child('+ parseInt(current) +')').find(':input:first').focus();	
    		});
            e.preventDefault();
        });
     
    	/*
    	clicking on the tab (on the last input of each fieldset), makes the form
    	slide to the next step
    	*/
    	$('#formElem > fieldset').each(function(){
    		var $fieldset = $(this);
    		$fieldset.children(':last').find(':input').keydown(function(e){
    			if (e.which == 9){
    				$('#navigation li:nth-child(' + (parseInt(current)+1) + ') a').click();
    				/* force the blur for validation */
    				$(this).blur();
    				e.preventDefault();
    			}
    		});
    	});
     
    	/*
    	validates errors on all the fieldsets
    	records if the Form has errors in $('#formElem').data()
    	*/
    	function validateSteps(){
    		var FormErrors = false;
    		for(var i = 1; i < fieldsetCount; ++i){
    			var error = validateStep(i);
    			if(error == -1)
    				FormErrors = true;
    		}
    		$('#formElem').data('errors',FormErrors);	
    	}
     
    	/*
    	validates one fieldset
    	and returns -1 if errors found, or 1 if not
    	*/
    	function validateStep(step){
    		if(step == fieldsetCount) return;
     
    		var error = 1;
    		var hasError = false;
    		$('#formElem').children(':nth-child('+ parseInt(step) +')').find(':input:not(button)').each(function(){
    			var $this 		= $(this);
    			var valueLength = jQuery.trim($this.val()).length;
     
    			if(valueLength == ''){
    				hasError = true;
    				$this.css('background-color','#FFEDEF');
    			}
    			else
    				$this.css('background-color','#FFFFFF');	
    		});
    		var $link = $('#navigation li:nth-child(' + parseInt(step) + ') a');
    		$link.parent().find('.error,.checked').remove();
     
    		var valclass = 'checked';
    		if(hasError){
    			error = -1;
    			valclass = 'error';
    		}
    		$('<span class="'+valclass+'"></span>').insertAfter($link);
     
    		return error;
    	}
     
    	/*
    	if there are errors don't allow the user to submit
    	*/
    	$('#registerButton').bind('click',function(){
    		if($('#formElem').data('errors')){
    			alert('Please correct the errors in the Form');
    			return false;
    		}	
    	});
    });
    Code html : 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
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <title>Formulaire inscription</title>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
            <meta name="description" content="Formulaire" />
            <meta name="keywords" content="inscription"/>
            <link rel="stylesheet" href="css/style.css" type="text/css" media="screen"/>
    		<script type="text/javascript" src="jquery.min.js"></script>
            <script type="text/javascript" src="sliding.form.js"></script>
        </head>
        <style>
            span.reference{
                position:fixed;
                left:5px;
                top:5px;
                font-size:10px;
                text-shadow:1px 1px 1px #fff;
            }
            span.reference a{
                color:#555;
                text-decoration:none;
                            text-transform:uppercase;
            }
            span.reference a:hover{
                color:#000;
                
            }
            h1{
                color:#ccc;
                font-size:36px;
                text-shadow:1px 1px 1px #fff;
                padding:20px;
            }
                    
        </style>
        <body>
            <div id="content">
                <h1>Inscription</h1>
                <div id="wrapper">
    				<div id="navigation" style="display:none;">
                        <ul>
                            <li class="selected">
                                <a href="#">1 - Contact &rarr;</a>
                            </li>
                            <li>
                                <a href="#">2 - Inscription &rarr;</a>
                            </li>
                            <li>
                                <a href="#">3 - Identification &rarr;</a>
                            </li>
                            <li>
                                <a href="#">4 - Newletter &rarr;</a>
                            </li>
    						<li>
                                <a href="#">5 - Validation</a>
                            </li>
                        </ul>
                    </div>
     
                    <div id="steps">
     
                        <form id="formElem" name="formElem" action="forminsc.php" method="post">
     
    						<fieldset class="step">
     
                                <titreform>Situation</titreform>
                                <p>
                                     <label for="pays">Pays</label>
                                    <select id="pays" name="pays">
    									<option>France</option>
                                    </select>
                                </p>
     
    							<titreform>Contacts</titreform>
                                <p>
                                    <label for="nom">Nom*</label>
                                    <input id="nom" name="nom" type="text" AUTOCOMPLETE=OFF />
                                </p>
     
                            </fieldset>
     
                            <fieldset class="step">
                                <titreform>Adresse</titreform>
    							<p>
                                    <label for="codepost">Code postale*</label>
                                    <input id="codepost" size="10" maxlength="10" name="codepost" type="text" AUTOCOMPLETE=OFF />
    							</p>
     
                            </fieldset>
                            <fieldset class="step">
                                <titreform>Identification</titreform>
     
                                <p>
                                    <label for="adresse">Adresse*</label>
                                    <input id="adresse" name="adresse" type="text" AUTOCOMPLETE=OFF />
                                </p>
    							<p>
                                    <label for="pre">prenom*</label>
                                    <input id="pre" name="pre" type="text" AUTOCOMPLETE=OFF />
                                </p>
    							<p>
                                    <label for="age">age*</label>
                                    <input id="age" name="age" type="text" AUTOCOMPLETE=OFF />
                                </p>
                            </fieldset>
                            <fieldset class="step">
                                <titreform>Newletter</titreform>
                                <p>
                                    <label for="newsletter">Newsletter</label>
                                    <select id="newsletter" name="newsletter">
                                        <option value="Daily" selected>Tous les jours</option>
                                        <option value="Weekly">1 fois par semaine</option>
                                        <option value="Monthly">1 fois par mois</option>
                                        <option value="Never">Jamais</option>
                                    </select>
                                </p>
                            </fieldset>
    						<fieldset class="step">
                                <legend>Validation</legend>
    							<p>
    								Si le formulaire a été correctement rempli
    								tout les onglets ont une coche verte.
    								Une coche rouge indique que certains champs
    								sont manquants ou rempli avec des données invalides.
    							</p>
                                <p class="submit">
                                    <button id="registerButton" type="submit">Valider mon inscription</button>
                                </p>
                            </fieldset>
                        </form>
                    </div>
                </div>
            </div>
        </body>
    </html>

  2. #2
    Rédacteur/Modérateur

    Avatar de SpaceFrog
    Homme Profil pro
    Développeur Web Php Mysql Html Javascript CSS Apache - Intégrateur - Bidouilleur SharePoint
    Inscrit en
    Mars 2002
    Messages
    39 641
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 74
    Localisation : Royaume-Uni

    Informations professionnelles :
    Activité : Développeur Web Php Mysql Html Javascript CSS Apache - Intégrateur - Bidouilleur SharePoint
    Secteur : Industrie

    Informations forums :
    Inscription : Mars 2002
    Messages : 39 641
    Points : 66 666
    Points
    66 666
    Billets dans le blog
    1
    Par défaut
    ajoute une class spécifique sur les champs que tu veux tester, et boucle sur la collection

  3. #3
    Membre à l'essai
    Profil pro
    Inscrit en
    Décembre 2013
    Messages
    30
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2013
    Messages : 30
    Points : 21
    Points
    21
    Par défaut
    bonjour,
    merci de votre reponse, pourriez vous me donner un exemple car je ne comprend pas
    merci

  4. #4
    Membre à l'essai
    Profil pro
    Inscrit en
    Décembre 2013
    Messages
    30
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2013
    Messages : 30
    Points : 21
    Points
    21
    Par défaut
    Après des relecture de ma bible js, la solution était toute bête
    Il suffit de rajouter input.requered:not(button)

    Et de mettre un id. dans le champ souhaité

    On peut clore le sujet

    Merci

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

Discussions similaires

  1. vérification de champ dans un formulaire
    Par didierledid dans le forum Général JavaScript
    Réponses: 8
    Dernier message: 26/12/2007, 09h26
  2. Vérification de champ dans un formulaire
    Par akara dans le forum Langage
    Réponses: 2
    Dernier message: 16/07/2007, 15h24
  3. Réponses: 4
    Dernier message: 09/01/2007, 22h28
  4. Code si modification sur champ dans formulaire
    Par guiguikawa dans le forum IHM
    Réponses: 1
    Dernier message: 14/06/2006, 15h31
  5. Masquer champs dans formulaire
    Par Michel Hubert dans le forum Access
    Réponses: 1
    Dernier message: 03/05/2006, 15h23

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