| 1 | <!DOCTYPE html> |
| 2 | <html> |
| 3 | <head> |
| 4 | <title>JQuery-validation demo | Bootstrap</title> |
| 5 | |
| 6 | <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" /> |
| 7 | |
| 8 | <script type="text/javascript" src="../../lib/jquery-1.11.1.js"></script> |
| 9 | <script type="text/javascript" src="../../dist/jquery.validate.js"></script> |
| 10 | </head> |
| 11 | <body> |
| 12 | <div class="container"> |
| 13 | <div class="row"> |
| 14 | <div class="col-sm-8 col-sm-offset-2"> |
| 15 | <div class="page-header"> |
| 16 | <div class="alert alert-info" role="alert"> |
| 17 | <h4>This demo shows how to integrate JQuery-validation and the Bootstrap framework.</h4> |
| 18 | <ul> |
| 19 | <li><a href="https://getbootstrap.com/" class="alert-link">Bootstrap home project</a>.</li> |
| 20 | </ul> |
| 21 | </div> |
| 22 | </div> |
| 23 | |
| 24 | <div class="panel panel-default"> |
| 25 | <div class="panel-heading"> |
| 26 | <h3 class="panel-title">Simple Form</h3> |
| 27 | </div> |
| 28 | <div class="panel-body"> |
| 29 | <form id="signupForm" method="post" class="form-horizontal" action=""> |
| 30 | <div class="form-group"> |
| 31 | <label class="col-sm-4 control-label" for="firstname">First name</label> |
| 32 | <div class="col-sm-5"> |
| 33 | <input type="text" class="form-control" id="firstname" name="firstname" placeholder="First name" /> |
| 34 | </div> |
| 35 | </div> |
| 36 | |
| 37 | <div class="form-group"> |
| 38 | <label class="col-sm-4 control-label" for="lastname">Last name</label> |
| 39 | <div class="col-sm-5"> |
| 40 | <input type="text" class="form-control" id="lastname" name="lastname" placeholder="Last name" /> |
| 41 | </div> |
| 42 | </div> |
| 43 | |
| 44 | <div class="form-group"> |
| 45 | <label class="col-sm-4 control-label" for="username">Username</label> |
| 46 | <div class="col-sm-5"> |
| 47 | <input type="text" class="form-control" id="username" name="username" placeholder="Username" /> |
| 48 | </div> |
| 49 | </div> |
| 50 | |
| 51 | <div class="form-group"> |
| 52 | <label class="col-sm-4 control-label" for="email">Email</label> |
| 53 | <div class="col-sm-5"> |
| 54 | <input type="text" class="form-control" id="email" name="email" placeholder="Email" /> |
| 55 | </div> |
| 56 | </div> |
| 57 | |
| 58 | <div class="form-group"> |
| 59 | <label class="col-sm-4 control-label" for="password">Password</label> |
| 60 | <div class="col-sm-5"> |
| 61 | <input type="password" class="form-control" id="password" name="password" placeholder="Password" /> |
| 62 | </div> |
| 63 | </div> |
| 64 | |
| 65 | <div class="form-group"> |
| 66 | <label class="col-sm-4 control-label" for="confirm_password">Confirm password</label> |
| 67 | <div class="col-sm-5"> |
| 68 | <input type="password" class="form-control" id="confirm_password" name="confirm_password" placeholder="Confirm password" /> |
| 69 | </div> |
| 70 | </div> |
| 71 | |
| 72 | <div class="form-group"> |
| 73 | <div class="col-sm-5 col-sm-offset-4"> |
| 74 | <div class="checkbox"> |
| 75 | <label> |
| 76 | <input type="checkbox" id="agree" name="agree" value="agree" />Please agree to our policy |
| 77 | </label> |
| 78 | </div> |
| 79 | </div> |
| 80 | </div> |
| 81 | |
| 82 | <div class="form-group"> |
| 83 | <div class="col-sm-9 col-sm-offset-4"> |
| 84 | <button type="submit" class="btn btn-primary" name="signup" value="Sign up">Sign up</button> |
| 85 | </div> |
| 86 | </div> |
| 87 | </form> |
| 88 | </div> |
| 89 | </div> |
| 90 | |
| 91 | <div class="panel panel-default"> |
| 92 | <div class="panel-heading"> |
| 93 | <h3 class="panel-title">Using feedback icons</h3> |
| 94 | </div> |
| 95 | <div class="panel-body"> |
| 96 | <form id="signupForm1" method="post" class="form-horizontal" action=""> |
| 97 | <div class="form-group"> |
| 98 | <label class="col-sm-4 control-label" for="firstname1">First name</label> |
| 99 | <div class="col-sm-5"> |
| 100 | <input type="text" class="form-control" id="firstname1" name="firstname1" placeholder="First name" /> |
| 101 | </div> |
| 102 | </div> |
| 103 | |
| 104 | <div class="form-group"> |
| 105 | <label class="col-sm-4 control-label" for="lastname1">Last name</label> |
| 106 | <div class="col-sm-5"> |
| 107 | <input type="text" class="form-control" id="lastname1" name="lastname1" placeholder="Last name" /> |
| 108 | </div> |
| 109 | </div> |
| 110 | |
| 111 | <div class="form-group"> |
| 112 | <label class="col-sm-4 control-label" for="username1">Username</label> |
| 113 | <div class="col-sm-5"> |
| 114 | <input type="text" class="form-control" id="username1" name="username1" placeholder="Username" /> |
| 115 | </div> |
| 116 | </div> |
| 117 | |
| 118 | <div class="form-group"> |
| 119 | <label class="col-sm-4 control-label" for="email1">Email</label> |
| 120 | <div class="col-sm-5"> |
| 121 | <input type="text" class="form-control" id="email1" name="email1" placeholder="Email" /> |
| 122 | </div> |
| 123 | </div> |
| 124 | |
| 125 | <div class="form-group"> |
| 126 | <label class="col-sm-4 control-label" for="password1">Password</label> |
| 127 | <div class="col-sm-5"> |
| 128 | <input type="password" class="form-control" id="password1" name="password1" placeholder="Password" /> |
| 129 | </div> |
| 130 | </div> |
| 131 | |
| 132 | <div class="form-group"> |
| 133 | <label class="col-sm-4 control-label" for="confirm_password1">Confirm password</label> |
| 134 | <div class="col-sm-5"> |
| 135 | <input type="password" class="form-control" id="confirm_password1" name="confirm_password1" placeholder="Confirm password" /> |
| 136 | </div> |
| 137 | </div> |
| 138 | |
| 139 | <div class="form-group"> |
| 140 | <div class="col-sm-5 col-sm-offset-4"> |
| 141 | <div class="checkbox"> |
| 142 | <label> |
| 143 | <input type="checkbox" id="agree1" name="agree1" value="agree" />Please agree to our policy |
| 144 | </label> |
| 145 | </div> |
| 146 | </div> |
| 147 | </div> |
| 148 | |
| 149 | <div class="form-group"> |
| 150 | <div class="col-sm-9 col-sm-offset-4"> |
| 151 | <button type="submit" class="btn btn-primary" name="signup1" value="Sign up">Sign up</button> |
| 152 | </div> |
| 153 | </div> |
| 154 | </form> |
| 155 | </div> |
| 156 | </div> |
| 157 | </div> |
| 158 | </div> |
| 159 | </div> |
| 160 | <script type="text/javascript"> |
| 161 | $.validator.setDefaults( { |
| 162 | submitHandler: function () { |
| 163 | alert( "submitted!" ); |
| 164 | } |
| 165 | } ); |
| 166 | |
| 167 | $( document ).ready( function () { |
| 168 | $( "#signupForm" ).validate( { |
| 169 | rules: { |
| 170 | firstname: "required", |
| 171 | lastname: "required", |
| 172 | username: { |
| 173 | required: true, |
| 174 | minlength: 2 |
| 175 | }, |
| 176 | password: { |
| 177 | required: true, |
| 178 | minlength: 5 |
| 179 | }, |
| 180 | confirm_password: { |
| 181 | required: true, |
| 182 | minlength: 5, |
| 183 | equalTo: "#password" |
| 184 | }, |
| 185 | email: { |
| 186 | required: true, |
| 187 | email: true |
| 188 | }, |
| 189 | agree: "required" |
| 190 | }, |
| 191 | messages: { |
| 192 | firstname: "Please enter your firstname", |
| 193 | lastname: "Please enter your lastname", |
| 194 | username: { |
| 195 | required: "Please enter a username", |
| 196 | minlength: "Your username must consist of at least 2 characters" |
| 197 | }, |
| 198 | password: { |
| 199 | required: "Please provide a password", |
| 200 | minlength: "Your password must be at least 5 characters long" |
| 201 | }, |
| 202 | confirm_password: { |
| 203 | required: "Please provide a password", |
| 204 | minlength: "Your password must be at least 5 characters long", |
| 205 | equalTo: "Please enter the same password as above" |
| 206 | }, |
| 207 | email: "Please enter a valid email address", |
| 208 | agree: "Please accept our policy" |
| 209 | }, |
| 210 | errorElement: "em", |
| 211 | errorPlacement: function ( error, element ) { |
| 212 | // Add the `help-block` class to the error element |
| 213 | error.addClass( "help-block" ); |
| 214 | |
| 215 | if ( element.prop( "type" ) === "checkbox" ) { |
| 216 | error.insertAfter( element.parent( "label" ) ); |
| 217 | } else { |
| 218 | error.insertAfter( element ); |
| 219 | } |
| 220 | }, |
| 221 | highlight: function ( element, errorClass, validClass ) { |
| 222 | $( element ).parents( ".col-sm-5" ).addClass( "has-error" ).removeClass( "has-success" ); |
| 223 | }, |
| 224 | unhighlight: function (element, errorClass, validClass) { |
| 225 | $( element ).parents( ".col-sm-5" ).addClass( "has-success" ).removeClass( "has-error" ); |
| 226 | } |
| 227 | } ); |
| 228 | |
| 229 | $( "#signupForm1" ).validate( { |
| 230 | rules: { |
| 231 | firstname1: "required", |
| 232 | lastname1: "required", |
| 233 | username1: { |
| 234 | required: true, |
| 235 | minlength: 2 |
| 236 | }, |
| 237 | password1: { |
| 238 | required: true, |
| 239 | minlength: 5 |
| 240 | }, |
| 241 | confirm_password1: { |
| 242 | required: true, |
| 243 | minlength: 5, |
| 244 | equalTo: "#password1" |
| 245 | }, |
| 246 | email1: { |
| 247 | required: true, |
| 248 | email: true |
| 249 | }, |
| 250 | agree1: "required" |
| 251 | }, |
| 252 | messages: { |
| 253 | firstname1: "Please enter your firstname", |
| 254 | lastname1: "Please enter your lastname", |
| 255 | username1: { |
| 256 | required: "Please enter a username", |
| 257 | minlength: "Your username must consist of at least 2 characters" |
| 258 | }, |
| 259 | password1: { |
| 260 | required: "Please provide a password", |
| 261 | minlength: "Your password must be at least 5 characters long" |
| 262 | }, |
| 263 | confirm_password1: { |
| 264 | required: "Please provide a password", |
| 265 | minlength: "Your password must be at least 5 characters long", |
| 266 | equalTo: "Please enter the same password as above" |
| 267 | }, |
| 268 | email1: "Please enter a valid email address", |
| 269 | agree1: "Please accept our policy" |
| 270 | }, |
| 271 | errorElement: "em", |
| 272 | errorPlacement: function ( error, element ) { |
| 273 | // Add the `help-block` class to the error element |
| 274 | error.addClass( "help-block" ); |
| 275 | |
| 276 | // Add `has-feedback` class to the parent div.form-group |
| 277 | // in order to add icons to inputs |
| 278 | element.parents( ".col-sm-5" ).addClass( "has-feedback" ); |
| 279 | |
| 280 | if ( element.prop( "type" ) === "checkbox" ) { |
| 281 | error.insertAfter( element.parent( "label" ) ); |
| 282 | } else { |
| 283 | error.insertAfter( element ); |
| 284 | } |
| 285 | |
| 286 | // Add the span element, if doesn't exists, and apply the icon classes to it. |
| 287 | if ( !element.next( "span" )[ 0 ] ) { |
| 288 | $( "<span class='glyphicon glyphicon-remove form-control-feedback'></span>" ).insertAfter( element ); |
| 289 | } |
| 290 | }, |
| 291 | success: function ( label, element ) { |
| 292 | // Add the span element, if doesn't exists, and apply the icon classes to it. |
| 293 | if ( !$( element ).next( "span" )[ 0 ] ) { |
| 294 | $( "<span class='glyphicon glyphicon-ok form-control-feedback'></span>" ).insertAfter( $( element ) ); |
| 295 | } |
| 296 | }, |
| 297 | highlight: function ( element, errorClass, validClass ) { |
| 298 | $( element ).parents( ".col-sm-5" ).addClass( "has-error" ).removeClass( "has-success" ); |
| 299 | $( element ).next( "span" ).addClass( "glyphicon-remove" ).removeClass( "glyphicon-ok" ); |
| 300 | }, |
| 301 | unhighlight: function ( element, errorClass, validClass ) { |
| 302 | $( element ).parents( ".col-sm-5" ).addClass( "has-success" ).removeClass( "has-error" ); |
| 303 | $( element ).next( "span" ).addClass( "glyphicon-ok" ).removeClass( "glyphicon-remove" ); |
| 304 | } |
| 305 | } ); |
| 306 | } ); |
| 307 | </script> |
| 308 | </body> |
| 309 | </html> |