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
| <!DOCTYPE html>
<html lang="fr" dir="ltr">
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1.0">
<meta name="author" content="Daniel Hagnoul">
<title>Forum jQuery</title>
<script src="http://cdnjs.cloudflare.com/ajax/libs/headjs/0.99/head.min.js"></script>
<script>
"use strict";
head.js(
"http://d3js.org/d3.v3.min.js",
"http://code.jquery.com/jquery-2.0.3.min.js",
"http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/i18n/jquery-ui-i18n.min.js",
"http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js",
"http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js",
"http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/additional-methods.min.js",
"http://danielhagnoul.developpez.com/lib/dvjh/d3Base.js", function(){
$( function(){
// message d'une seule rubrique
$.validator.addMethod( 'required_group', function( val, el ){
var $module = $(el).parents('div.side_right'),
contientPlaceHolder = true,
nbreChpAvecPlaceHolder = 0;
if ( $( '#txt_montantfix' ).val() != $( '#txt_montantfix' ).attr( 'placeholder' ) && $( '#txt_montantfix' ).val() != '' ){
nbreChpAvecPlaceHolder++ ;
}
if ( $( '#dateOp' ).val() != $( '#dateOp' ).attr( 'placeholder' ) && $( '#dateOp' ).val() != '' ){
nbreChpAvecPlaceHolder++ ;
}
if ( $( '#numCheck' ).val() != '' ){
nbreChpAvecPlaceHolder++ ;
}
if ( nbreChpAvecPlaceHolder > 1 ){
contientPlaceHolder = false;
}
return contientPlaceHolder ;
}, "veuillez renseigner une seule rubrique" );
// validation format date
$.validator.addMethod( "optdate", function( value, element ){
var check = false,
pattern = /^\d{2,2}\/\d{2,2}\/\d{4}$/;
if ( pattern.test( value ) ){
var adata = value.split( '/' ),
dd = parseInt( adata[ 0 ], 10 ),
mm = parseInt( adata[ 1 ], 10 ),
yyyy = parseInt( adata[ 2 ], 10 ),
xdata = new Date( yyyy, mm - 1, dd );
if ( ( xdata.getFullYear() == yyyy ) && ( xdata.getMonth () == mm - 1 ) && ( xdata.getDate() == dd ) ){
check = true;
} else {
check = false;
}
} else {
check = false;
}
return this.optional( element ) || check;
}, "Please enter a valid date." );
// validation format montant
$.validator.addMethod( "montantfmt", function( value, element ){
var check = true,
pattern =/^[-+]?[0-9]{1,10}([,\.][0-9]{1,2})?$/;
if ( value != "" && ! pattern.test( value ) ){
check = false;
}
return check;
}, "Veuillez saisir un montant composé au maximum de 10 chiffres avant la virgule et de 2 chiffres après la virgule.." );
$( "#formID" ).validate({
"debug" : true,
"invalidHandler" : function( event, validator ){
// 'this' refers to the form
var errors = validator.numberOfInvalids();
if ( errors ){
var message = ( errors == 1 ) ?
'You missed 1 field. It has been highlighted' :
'You missed ' + errors + ' fields. They have been highlighted';
$( "div.errorForm-msg span" ).html( message );
$( "div.errorForm-msg" ).show();
} else {
$( "div.errorForm-msg" ).hide();
}
}
});
$( "input", "#formID" ).on( "keyup change blur", function(){
$( "#formID" ).valid();
});
/*
J'ai un formulaire avec 3 champs de textes,
J'ai deux règles :
- Un seul champ doit être rempli à la fois.
- Validation de format des champs remplis
Les messages d'erreur sont affichés en haut de la page.
S'il y a deux champs remplis ou plus ( j'affiche un message: "veuillez saisir une seule rubrique" )
Si je remplis un champ avec un format incorrect ( j'affiche soit un message
"veuillez saisir une seule rubrique" si d'autres champs remplis ou "format incorrect" )
Mon problème est le suivant :
Quand j'ai deux champs avec formats incorrects, et que j'efface l'un de ces deux champs,
je perds mon message d'erreur alors qu'il doit m'afficher un nouveau message celui du format incorrect
*/
});
$( window ).load( function(){
});
});
</script>
<link href='http://fonts.googleapis.com/css?family=Sofia|Ubuntu:400|Kreon'>
<link rel="stylesheet" href="http://danielhagnoul.developpez.com/styles/dvjhRemBase.css">
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/themes/sunny/jquery-ui.min.css">
<style>
/* TEST -- Nota bene : ici 1 rem est égal à 1 px, voir dvjhRemBase.css */
</style>
</head>
<body>
<header>
<hgroup>
<h1>Forum jQuery</h1>
<h2>
<a href="">Lien</a>
</h2>
</hgroup>
</header>
<section class="conteneur">
<div id="anchorDiv"></div>
<div class="side_right grid_12">
<div class="errorForm-msg">
<span class="arrow-info-btm"></span>
<p>Veuillez vérifier les informations saisies :</p>
</div>
</div>
<form id="formID">
<fieldset>
<legend class="hors-ecran">recherche par montant</legend>
<table width="727" border="0" cellspacing="0" cellpadding="0" class="table-recherche">
<caption><h2 class="tt_compte">recherche par montant</h2></caption>
<thead>
<tr>
<th scope="row" class="tt-head-table">
<h2 class="tt_compte">recherche par montant</h2>
</th>
</tr>
</thead>
<tbody>
<tr>
<td class="group-border">
<p class="zoneMontantFix zoneMontantFixDevise">
<label for="txt_montantfix">Montant fixe :</label>
<input id="txt_montantfix" name="amountOp" class="required_group montantfmt" placeholder="0000,00" type="text" type="text" value=""/> <span>EUR</span>
</p>
</td>
</tr>
</tbody>
</table>
</fieldset>
<fieldset>
<legend class="hors-ecran">recherche par date d’opération</legend>
<table width="727" border="0" cellspacing="0" cellpadding="0" class="table-recherche">
<caption>recherche par date d’opération</caption>
<thead>
<tr>
<th scope="row" class="tt-head-table">
<h2 class="tt_compte">recherche par date d’opération</h2>
</th>
</tr>
</thead>
<tbody>
<tr>
<td class="group-border">
<div class="zoneDateOperation">
<p class="zoneMontantFix zoneMontantFixDevise">
<label for="dateOp">Date d’opération :</label>
<input id="dateOp" name="dateOp" class="required_group optdate" placeholder="JJ/MM/AAAA" type="text" type="text" value=""/>
</p>
</div>
</td>
</tr>
</tbody>
</table>
</fieldset>
<fieldset>
<legend class="hors-ecran">recherche par numéro de chèque</legend>
<table width="727" border="0" cellspacing="0" cellpadding="0" class="table-recherche">
<caption>recherche par numéro de chèque</caption>
<thead>
<tr>
<th scope="row" class="tt-head-table">
<h2 class="tt_compte">recherche par numéro de chèque</h2>
</th>
</tr>
</thead>
<tbody>
<tr>
<td class="group-border">
<div class="zoneNumeroCheque">
<p class="zoneMontantFix zoneMontantFixDevise">
<label for="numCheck">Numéro de chèque :</label>
<input id="numCheck" name="numCheck" class="required_group" type="text" type="text" value=""/>
</p>
</div>
</td>
</tr>
</tbody>
</table>
</fieldset>
<input type="submit" value="Submit">
</form>
</section>
<footer itemscope itemtype="http://danielhagnoul.developpez.com/">
<time datetime="2013-08-22T10:03:02.849+02:00" pubdate>2013-08-22T10:03:02.849+02:00</time>
<span itemprop="name">Daniel Hagnoul</span>
<a href="http://www.developpez.net/forums/u285162/danielhagnoul/" itemprop="url">@danielhagnoul</a>
<a href="http://danielhagnoul.developpez.com/" itemprop="url">Mon cahier dexercices</a>
<a href="http://javascript.developpez.com/faq/jquery/" itemprop="url">FAQ</a>
<a href="http://javascript.developpez.com/cours/?page=frameworks#jquery" itemprop="url">Tutoriels</a>
</footer>
</body>
</html> |
Partager