|
| 1 | +# Form validator plugin for Bootstrap # |
| 2 | + |
| 3 | +This plugin verifies if the user doesn't write errors in the form field according to the Bootstrap styles. |
| 4 | +The form rules are defined with the HTML5 specifications. |
| 5 | + |
| 6 | +Here is an example which shows a form with all input types. |
| 7 | + |
| 8 | +Installation |
| 9 | +------------ |
| 10 | + |
| 11 | +First, to make it works, the following files have to be included. |
| 12 | + |
| 13 | +<pre> |
| 14 | +<link href="bootstrap-default.css" rel="stylesheet"> |
| 15 | +<link href="bootstrap-responsive.css" rel="stylesheet"> |
| 16 | +<link href="bootstrap-form.css" rel="stylesheet"> |
| 17 | +<script src="jquery.js"> </script> |
| 18 | +<script src="bootstrap-tooltip.js"> </script> |
| 19 | +<script src="bootstrap-form.js"> </script> |
| 20 | +</pre> |
| 21 | +*Note : Files `bootstrap-form.css` and `bootstrap-form.js` are part of the plugin.* |
| 22 | + |
| 23 | +Launching |
| 24 | +--------- |
| 25 | + |
| 26 | +Next, the form validator plugin must launch. |
| 27 | + |
| 28 | +<pre> |
| 29 | +<script type="text/javascript"> |
| 30 | + $(document).ready(function() { |
| 31 | + $('form').form(); |
| 32 | + }); |
| 33 | +</script> |
| 34 | +</pre> |
| 35 | + |
| 36 | +It is possible to specify options when the form is launched : |
| 37 | +* asterisk, boolean which print or not the asterisk for obligatory field, `true` ; |
| 38 | +* asteriskContent, string which is the asterisk content, `<span class='help-inline'>*</span>`. |
| 39 | + |
| 40 | +Example |
| 41 | +------- |
| 42 | + |
| 43 | +To finish, in first part, here is a screen for a form example. |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | +And, in second part, the form body with all HTML5 input types which built this example. |
| 48 | + |
| 49 | +<pre> |
| 50 | +<form class="form form-horizontal" action="./"> |
| 51 | +</pre> |
| 52 | + |
| 53 | +<pre> |
| 54 | + <fieldset class="well"> |
| 55 | + <h3>Connection informations</h3> |
| 56 | + <br/> |
| 57 | +</pre> |
| 58 | + |
| 59 | +**Input text** |
| 60 | + |
| 61 | +<pre> |
| 62 | + <div class="control-group"> |
| 63 | + <label class="control-label" for="inputLogin">Login</label> |
| 64 | + <div class="controls"> |
| 65 | + <input type="text" id="inputLogin" class="input-medium" required="required" |
| 66 | + pattern="[A-Za-z0-9]*" data-error="Please give a correct login."> |
| 67 | + </div> |
| 68 | + </div> |
| 69 | +</pre> |
| 70 | +If the user doesn't want to show an error on this input, |
| 71 | + |
| 72 | +- he mustn't let the empty field `required="required"` ; |
| 73 | +- he has to write letters or numbers `pattern="[A-Za-z0-9]*"`. |
| 74 | + |
| 75 | +Else, the error defined with `data-error="Please give a correct login."` appears. |
| 76 | + |
| 77 | +**Input password** |
| 78 | + |
| 79 | +<pre> |
| 80 | + <div class="control-group"> |
| 81 | + <label class="control-label" for="inputPassword">Password</label> |
| 82 | + <div class="controls"> |
| 83 | + <input type="password" id="inputPassword" class="input-medium" required="required" |
| 84 | + data-error="Please give a correct password."> |
| 85 | + </div> |
| 86 | + </div> |
| 87 | +</pre> |
| 88 | +If the user doesn't want to show an error on this input, |
| 89 | + |
| 90 | +- he mustn't let the empty field `required="required"`. |
| 91 | + |
| 92 | +Else, the error defined with `data-error="Please give a correct password."` appears. |
| 93 | + |
| 94 | +**Input email** |
| 95 | + |
| 96 | +<pre> |
| 97 | + <div class="control-group"> |
| 98 | + <label class="control-label" for="inputEmail">E-mail address</label> |
| 99 | + <div class="controls"> |
| 100 | + <input type="email" id="inputEmail" class="input-medium" required="required" |
| 101 | + data-error="Please give a correct e-mail address."> |
| 102 | + </div> |
| 103 | + </div> |
| 104 | +</pre> |
| 105 | +If the user doesn't want to show an error on this input, |
| 106 | + |
| 107 | +- he has to write the email format `type="email"` ; |
| 108 | +- he mustn't let the empty field `required="required"`. |
| 109 | + |
| 110 | +Else, the error defined with `data-error="Please give a correct e-mail address."` appears. |
| 111 | + |
| 112 | +<pre> |
| 113 | + </fieldset> |
| 114 | + <fieldset class="well"> |
| 115 | + <h3>General informations</h3> |
| 116 | + <br/> |
| 117 | +</pre> |
| 118 | + |
| 119 | +**Input radio** |
| 120 | + |
| 121 | +<pre> |
| 122 | + <div class="control-group"> |
| 123 | + <label class="control-label" for="inputGender">Gender</label> |
| 124 | + <div class="controls radio input-mini"> |
| 125 | + <input type="radio" value="Man" id="man" name="gender" required="required" |
| 126 | + data-error="Please check a gender."> |
| 127 | + <label for="man">Man</label> |
| 128 | + </div> |
| 129 | + <div class="controls radio input-mini"> |
| 130 | + <input type="radio" value="Woman" id="woman" name="gender"> |
| 131 | + <label for="woman">Woman</label> |
| 132 | + </div> |
| 133 | + </div> |
| 134 | +</pre> |
| 135 | +If the user doesn't want to show an error on this input, |
| 136 | + |
| 137 | +- he has to check one of all inputs `required="required"`. |
| 138 | + |
| 139 | +Else, the error defined with `data-error="Please check a gender."` appears. |
| 140 | + |
| 141 | +**Input date** |
| 142 | + |
| 143 | +<pre> |
| 144 | + <div class="control-group"> |
| 145 | + <label class="control-label" for="inputAge">Birth date</label> |
| 146 | + <div class="controls"> |
| 147 | + <input type="date" id="inputAge" class="input-medium" required="required" |
| 148 | + data-error="Please give a correct birth date."> |
| 149 | + </div> |
| 150 | + </div> |
| 151 | +</pre> |
| 152 | +If the user doesn't want to show an error on this input, |
| 153 | + |
| 154 | +- he has to write the date format `type="date"` ; |
| 155 | +- he mustn't let the empty field `required="required"`. |
| 156 | + |
| 157 | +Else, the error defined with `data-error="Please give a correct birth date."` appears. |
| 158 | + |
| 159 | +**TextArea** |
| 160 | + |
| 161 | +<pre> |
| 162 | + <div class="control-group"> |
| 163 | + <label class="control-label" for="inputLocation">Locality<br/>ZIP<br/>City</label> |
| 164 | + <div class="controls"> |
| 165 | + <textarea id="inputLocation" class="input-medium" rows="3"></textarea> |
| 166 | + </div> |
| 167 | + </div> |
| 168 | +</pre> |
| 169 | +The user never watch errors on this input. |
| 170 | + |
| 171 | +**Input tel** |
| 172 | + |
| 173 | +<pre> |
| 174 | + <div class="control-group"> |
| 175 | + <label class="control-label" for="inputTel">Phone</label> |
| 176 | + <div class="controls"> |
| 177 | + <input type="tel" id="inputTel" class="input-medium" pattern="[0-9]{10}" |
| 178 | + data-error="Please give a correct phone number."> |
| 179 | + </div> |
| 180 | + </div> |
| 181 | +</pre> |
| 182 | +If the user doesn't want to show an error on this input, |
| 183 | + |
| 184 | +- he has to write the telephone number format `type="tel"`. |
| 185 | + |
| 186 | +Else, the error defined with `data-error="Please give a correct phone number."` appears. |
| 187 | + |
| 188 | +**Input url** |
| 189 | + |
| 190 | +<pre> |
| 191 | + <div class="control-group"> |
| 192 | + <label class="control-label" for="inputWeb">Website</label> |
| 193 | + <div class="controls"> |
| 194 | + <input type="url" id="inputWeb" class="input-medium" placeholder="http://" |
| 195 | + data-error="Please give a correct website url."> |
| 196 | + </div> |
| 197 | + </div> |
| 198 | +</pre> |
| 199 | +If the user doesn't want to show an error on this input, |
| 200 | + |
| 201 | +- he has to write the website url format `type="url"`. |
| 202 | + |
| 203 | +Else, the error defined with `data-error="Please give a correct website url."` appears. |
| 204 | + |
| 205 | +**Input file** |
| 206 | + |
| 207 | +<pre> |
| 208 | + <div class="control-group"> |
| 209 | + <label class="control-label" for="inputImage">Personal image</label> |
| 210 | + <div class="controls"> |
| 211 | + <input type="file" id="inputImage" class="input-xlarge" accept="image/jpeg, image/png" |
| 212 | + required="required" |
| 213 | + data-error="Please give an image with jpg or png format."> |
| 214 | + </div> |
| 215 | + </div> |
| 216 | +</pre> |
| 217 | +If the user doesn't want to show an error on this input, |
| 218 | + |
| 219 | +- he has to choose good files `type="file"` and `accept="image/jpeg, image/png"` ; |
| 220 | +- he mustn't let the empty field `required="required"`. |
| 221 | + |
| 222 | +Else, the error defined with `data-error="Please give an image with jpg or png format."` appears. |
| 223 | + |
| 224 | +<pre> |
| 225 | + </fieldset> |
| 226 | + <fieldset class="well"> |
| 227 | + <h3>Optional informations</h3> |
| 228 | + <br/> |
| 229 | +</pre> |
| 230 | + |
| 231 | +**Input number** |
| 232 | + |
| 233 | +<pre> |
| 234 | + <div class="control-group"> |
| 235 | + <label class="control-label" for="inputComputer">Computers number</label> |
| 236 | + <div class="controls"> |
| 237 | + <input type="number" value="0" min="0" max="10" id="inputComputer" class="input-medium" |
| 238 | + data-error="Please give a correct computers number."> |
| 239 | + </div> |
| 240 | + </div> |
| 241 | +</pre> |
| 242 | +If the user doesn't want to show an error on this input, |
| 243 | + |
| 244 | +- he has to choose good number `type="number"`, `min="0"` and `max="10"`. |
| 245 | + |
| 246 | +Else, the error defined with `data-error="Please give a correct computers number."` appears. |
| 247 | + |
| 248 | +**Input search** |
| 249 | + |
| 250 | +<pre> |
| 251 | + <div class="control-group"> |
| 252 | + <label class="control-label" for="inputSearch">Favorite animal</label> |
| 253 | + <div class="controls"> |
| 254 | + <input type="search" id="inputSearch" list="inputSearchList" class="input-medium"> |
| 255 | + <datalist id="inputSearchList"> |
| 256 | + <option value="Alligator"> |
| 257 | + <option value="Bear"> |
| 258 | + <option value="Cat"> |
| 259 | + <option value="Dolphin"> |
| 260 | + <option value="Elephant"> |
| 261 | + <option value="Fawn"> |
| 262 | + <option value="Gazelle"> |
| 263 | + </datalist> |
| 264 | + </div> |
| 265 | + </div> |
| 266 | +</pre> |
| 267 | +The user never watch errors on this input. |
| 268 | + |
| 269 | +**Select multiple** |
| 270 | + |
| 271 | +<pre> |
| 272 | + <div class="control-group"> |
| 273 | + <label class="control-label" for="inputFruit">Favorite fruit<br/>(between 2 and 4)</label> |
| 274 | + <div class="controls"> |
| 275 | + <select id="inputFruit" class="input-medium" multiple="multiple" data-min="2" data-max="4" |
| 276 | + data-error="Please choose between two and four fruits."> |
| 277 | + <option value="apple">Apple</option> |
| 278 | + <option value="banana">Banana</option> |
| 279 | + <option value="cherry">Cherry</option> |
| 280 | + <option value="dewberry">Dewberry</option> |
| 281 | + <option value="elderberry">Elderberry</option> |
| 282 | + <option value="fig">Fig</option> |
| 283 | + <option value="grape">Grape</option> |
| 284 | + </select> |
| 285 | + </div> |
| 286 | + </div> |
| 287 | +</pre> |
| 288 | +If the user doesn't want to show an error on this input, |
| 289 | + |
| 290 | +- he has to choose the asked number of all inputs `data-min="2"` and `data-min="4"`. |
| 291 | + |
| 292 | +Else, the error defined with `data-error="Please choose between two and four fruits."` appears. |
| 293 | + |
| 294 | +*Note : Tags `data-min` and `data-min` are specific tags for the form validator plugin. |
| 295 | +They impose a limit when the user has to make a multiple choice.* |
| 296 | + |
| 297 | +**Input checkbox** |
| 298 | + |
| 299 | +<pre> |
| 300 | + <div class="control-group"> |
| 301 | + <label class="control-label" for="inputBrowser">Favorite browser<br/>(at least 1)</label> |
| 302 | + <div class="controls checkbox input-mini"> |
| 303 | + <input type="checkbox" value="Chrome" id="chrome" name="browser[]" data-min="1" |
| 304 | + data-error="Please check at least one browser."> |
| 305 | + <label for="chrome">Chrome</label> |
| 306 | + </div> |
| 307 | + <div class="controls checkbox input-mini"> |
| 308 | + <input type="checkbox" value="Firefox" id="firefox" name="browser[]"> |
| 309 | + <label for="firefox">Firefox</label> |
| 310 | + </div> |
| 311 | + <div class="controls checkbox input-mini"> |
| 312 | + <input type="checkbox" value="IExplorer" id="iexplorer" name="browser[]"> |
| 313 | + <label for="iexplorer">IExplorer</label> |
| 314 | + </div> |
| 315 | + <div class="controls checkbox input-mini"> |
| 316 | + <input type="checkbox" value="Opera" id="opera" name="browser[]"> |
| 317 | + <label for="opera">Opera</label> |
| 318 | + </div> |
| 319 | + <div class="controls checkbox input-mini"> |
| 320 | + <input type="checkbox" value="Safari" id="safari" name="browser[]"> |
| 321 | + <label for="safari">Safari</label> |
| 322 | + </div> |
| 323 | + </div> |
| 324 | +</pre> |
| 325 | +If the user doesn't want to show an error on this input, |
| 326 | + |
| 327 | +- he has to check the asked number of all inputs `data-min="1"` (see **Select Multiple**). |
| 328 | + |
| 329 | +Else, the error defined with `data-error="Please check at least one browser."` appears. |
| 330 | + |
| 331 | +**Input color** |
| 332 | + |
| 333 | +<pre> |
| 334 | + <div class="control-group"> |
| 335 | + <label class="control-label" for="inputColor">Favorite color</label> |
| 336 | + <div class="controls"> |
| 337 | + <input type="color" id="inputColor" class="input-medium"> |
| 338 | + </div> |
| 339 | + </div> |
| 340 | +</pre> |
| 341 | +The user never watch errors on this input. |
| 342 | + |
| 343 | +**Select** |
| 344 | + |
| 345 | +<pre> |
| 346 | + <div class="control-group"> |
| 347 | + <label class="control-label" for="inputOS">Favorite OS</label> |
| 348 | + <div class="controls"> |
| 349 | + <select id="inputOS" class="input-medium"> |
| 350 | + <option>Nothing</option> |
| 351 | + <option>Linux</option> |
| 352 | + <option>Mac</option> |
| 353 | + <option>Windows</option> |
| 354 | + </select> |
| 355 | + </div> |
| 356 | + </div> |
| 357 | +</pre> |
| 358 | +The user never watch errors on this input. |
| 359 | + |
| 360 | +**Input range** |
| 361 | + |
| 362 | +<pre> |
| 363 | + <div class="control-group"> |
| 364 | + <label class="control-label" for="inputNote">Note</br>(on 20)</label> |
| 365 | + <div class="controls"> |
| 366 | + <input type="range" max="20" id="inputNote" class="input-medium"> |
| 367 | + </div> |
| 368 | + </div> |
| 369 | +</pre> |
| 370 | +The user never watch errors on this input. |
| 371 | + |
| 372 | +<pre> |
| 373 | + </fieldset> |
| 374 | +</pre> |
| 375 | + |
| 376 | +**Input button** |
| 377 | + |
| 378 | +<pre> |
| 379 | + <div class="form-actions"> |
| 380 | + <button type="submit" class="btn btn-primary">Submit</button> |
| 381 | + <input type="reset" class="btn" value="Cancel"> |
| 382 | + </div> |
| 383 | +</pre> |
| 384 | +When the user submit the form whereas fields are empty, the request is not sent and the errors printed. |
| 385 | +If he clicks on Cancel, the form resets like to the beginning. |
| 386 | + |
| 387 | +<pre> |
| 388 | +</form> |
| 389 | +</pre> |
0 commit comments