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:
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:
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 →</a>
</li>
<li>
<a href="#">2 - Inscription →</a>
</li>
<li>
<a href="#">3 - Identification →</a>
</li>
<li>
<a href="#">4 - Newletter →</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> |