经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » HTMLCSS » CSS » 查看文章
Bootstrap表单验证插件bootstrapValidator使用方法整理
来源:cnblogs  作者:子钦加油  时间:2018/10/20 15:09:19  对本文有异议

插件下载:http://www.jq22.com/jquery-info522

插件介绍

先上一个图:


 

下载地址:https://github.com/nghuuphuoc/bootstrapvalidator

使用方法:http://www.cnblogs.com/huangcong/p/5335376.html

 

使用提示

中文化:
下载插件后,将\js\bootstrapValidator\language\zh_CN.js 引入文件,即实现中文化

提交前验证表单:
更丰富一点的表单验证例子:http://www.jq22.com/yanshi522,直接上代码:

 

  1. 1 <!DOCTYPE html>
  2. 2 <html>
  3. 3 <head>
  4. 4 <title>BootstrapValidator demo</title>
  5. 5
  6. 6 <link rel="stylesheet" href="vendor/bootstrap/css/bootstrap.css"/>
  7. 7 <link rel="stylesheet" href="dist/css/bootstrapValidator.css"/>
  8. 8
  9. 9 <!-- Include the FontAwesome CSS if you want to use feedback icons provided by FontAwesome -->
  10. 10 <!--<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" />-->
  11. 11
  12. 12 <script type="text/javascript" src="vendor/jquery/jquery-1.10.2.min.js"></script>
  13. 13 <script type="text/javascript" src="vendor/bootstrap/js/bootstrap.min.js"></script>
  14. 14 <script type="text/javascript" src="dist/js/bootstrapValidator.js"></script>
  15. 15 </head>
  16. 16 <body>
  17. 17 <div class="container">
  18. 18 <div class="row">
  19. 19 <!-- form: -->
  20. 20 <section>
  21. 21 <div class="col-lg-8 col-lg-offset-2">
  22. 22 <div class="page-header">
  23. 23 <h2>Sign up</h2>
  24. 24 </div>
  25. 25
  26. 26 <form id="defaultForm" method="post" class="form-horizontal" action="target.php">
  27. 27 <div class="form-group">
  28. 28 <label class="col-lg-3 control-label">Full name</label>
  29. 29 <div class="col-lg-4">
  30. 30 <input type="text" class="form-control" name="firstName" placeholder="First name" />
  31. 31 </div>
  32. 32 <div class="col-lg-4">
  33. 33 <input type="text" class="form-control" name="lastName" placeholder="Last name" />
  34. 34 </div>
  35. 35 </div>
  36. 36
  37. 37 <div class="form-group">
  38. 38 <label class="col-lg-3 control-label">Username</label>
  39. 39 <div class="col-lg-5">
  40. 40 <input type="text" class="form-control" name="username" />
  41. 41 </div>
  42. 42 </div>
  43. 43
  44. 44 <div class="form-group">
  45. 45 <label class="col-lg-3 control-label">Email address</label>
  46. 46 <div class="col-lg-5">
  47. 47 <input type="text" class="form-control" name="email" />
  48. 48 </div>
  49. 49 </div>
  50. 50
  51. 51 <div class="form-group">
  52. 52 <label class="col-lg-3 control-label">Password</label>
  53. 53 <div class="col-lg-5">
  54. 54 <input type="password" class="form-control" name="password" />
  55. 55 </div>
  56. 56 </div>
  57. 57
  58. 58 <div class="form-group">
  59. 59 <label class="col-lg-3 control-label">Retype password</label>
  60. 60 <div class="col-lg-5">
  61. 61 <input type="password" class="form-control" name="confirmPassword" />
  62. 62 </div>
  63. 63 </div>
  64. 64
  65. 65 <div class="form-group">
  66. 66 <label class="col-lg-3 control-label">Gender</label>
  67. 67 <div class="col-lg-5">
  68. 68 <div class="radio">
  69. 69 <label>
  70. 70 <input type="radio" name="gender" value="male" /> Male
  71. 71 </label>
  72. 72 </div>
  73. 73 <div class="radio">
  74. 74 <label>
  75. 75 <input type="radio" name="gender" value="female" /> Female
  76. 76 </label>
  77. 77 </div>
  78. 78 <div class="radio">
  79. 79 <label>
  80. 80 <input type="radio" name="gender" value="other" /> Other
  81. 81 </label>
  82. 82 </div>
  83. 83 </div>
  84. 84 </div>
  85. 85
  86. 86 <div class="form-group">
  87. 87 <label class="col-lg-3 control-label">Birthday</label>
  88. 88 <div class="col-lg-5">
  89. 89 <input type="text" class="form-control" name="birthday" /> (YYYY/MM/DD)
  90. 90 </div>
  91. 91 </div>
  92. 92
  93. 93 <div class="form-group">
  94. 94 <label class="col-lg-3 control-label">Languages</label>
  95. 95 <div class="col-lg-5">
  96. 96 <div class="checkbox">
  97. 97 <label>
  98. 98 <input type="checkbox" name="languages[]" value="english" /> English
  99. 99 </label>
  100. 100 </div>
  101. 101 <div class="checkbox">
  102. 102 <label>
  103. 103 <input type="checkbox" name="languages[]" value="french" /> French
  104. 104 </label>
  105. 105 </div>
  106. 106 <div class="checkbox">
  107. 107 <label>
  108. 108 <input type="checkbox" name="languages[]" value="german" /> German
  109. 109 </label>
  110. 110 </div>
  111. 111 <div class="checkbox">
  112. 112 <label>
  113. 113 <input type="checkbox" name="languages[]" value="russian" /> Russian
  114. 114 </label>
  115. 115 </div>
  116. 116 <div class="checkbox">
  117. 117 <label>
  118. 118 <input type="checkbox" name="languages[]" value="other" /> Other
  119. 119 </label>
  120. 120 </div>
  121. 121 </div>
  122. 122 </div>
  123. 123
  124. 124 <div class="form-group">
  125. 125 <label class="col-lg-3 control-label">Programming Languages</label>
  126. 126 <div class="col-lg-5">
  127. 127 <div class="checkbox">
  128. 128 <label>
  129. 129 <input type="checkbox" name="programs[]" value="net" /> .Net
  130. 130 </label>
  131. 131 </div>
  132. 132 <div class="checkbox">
  133. 133 <label>
  134. 134 <input type="checkbox" name="programs[]" value="java" /> Java
  135. 135 </label>
  136. 136 </div>
  137. 137 <div class="checkbox">
  138. 138 <label>
  139. 139 <input type="checkbox" name="programs[]" value="c" /> C/C++
  140. 140 </label>
  141. 141 </div>
  142. 142 <div class="checkbox">
  143. 143 <label>
  144. 144 <input type="checkbox" name="programs[]" value="php" /> PHP
  145. 145 </label>
  146. 146 </div>
  147. 147 <div class="checkbox">
  148. 148 <label>
  149. 149 <input type="checkbox" name="programs[]" value="perl" /> Perl
  150. 150 </label>
  151. 151 </div>
  152. 152 <div class="checkbox">
  153. 153 <label>
  154. 154 <input type="checkbox" name="programs[]" value="ruby" /> Ruby
  155. 155 </label>
  156. 156 </div>
  157. 157 <div class="checkbox">
  158. 158 <label>
  159. 159 <input type="checkbox" name="programs[]" value="python" /> Python
  160. 160 </label>
  161. 161 </div>
  162. 162 <div class="checkbox">
  163. 163 <label>
  164. 164 <input type="checkbox" name="programs[]" value="javascript" /> Javascript
  165. 165 </label>
  166. 166 </div>
  167. 167 </div>
  168. 168 </div>
  169. 169
  170. 170 <div class="form-group">
  171. 171 <label class="col-lg-3 control-label" id="captchaOperation"></label>
  172. 172 <div class="col-lg-2">
  173. 173 <input type="text" class="form-control" name="captcha" />
  174. 174 </div>
  175. 175 </div>
  176. 176
  177. 177 <div class="form-group">
  178. 178 <div class="col-lg-9 col-lg-offset-3">
  179. 179 <button type="submit" class="btn btn-primary" name="signup" value="Sign up">Sign up</button>
  180. 180 <button type="submit" class="btn btn-primary" name="signup2" value="Sign up 2">Sign up 2</button>
  181. 181 <button type="button" class="btn btn-info" id="validateBtn">Manual validate</button>
  182. 182 <button type="button" class="btn btn-info" id="resetBtn">Reset form</button>
  183. 183 </div>
  184. 184 </div>
  185. 185 </form>
  186. 186 </div>
  187. 187 </section>
  188. 188 <!-- :form -->
  189. 189 </div>
  190. 190 </div>
  191. 191
  192. 192 <script type="text/javascript">
  193. 193 $(document).ready(function() {
  194. 194 // Generate a simple captcha
  195. 195 function randomNumber(min, max) {
  196. 196 return Math.floor(Math.random() * (max - min + 1) + min);
  197. 197 };
  198. 198 $('#captchaOperation').html([randomNumber(1, 100), '+', randomNumber(1, 200), '='].join(' '));
  199. 199
  200. 200 $('#defaultForm').bootstrapValidator({
  201. 201 // live: 'disabled',
  202. 202 message: 'This value is not valid',
  203. 203 feedbackIcons: {
  204. 204 valid: 'glyphicon glyphicon-ok',
  205. 205 invalid: 'glyphicon glyphicon-remove',
  206. 206 validating: 'glyphicon glyphicon-refresh'
  207. 207 },
  208. 208 fields: {
  209. 209 firstName: {
  210. 210 validators: {
  211. 211 notEmpty: {
  212. 212 message: 'The first name is required and cannot be empty'
  213. 213 }
  214. 214 }
  215. 215 },
  216. 216 lastName: {
  217. 217 validators: {
  218. 218 notEmpty: {
  219. 219 message: 'The last name is required and cannot be empty'
  220. 220 }
  221. 221 }
  222. 222 },
  223. 223 username: {
  224. 224 message: 'The username is not valid',
  225. 225 validators: {
  226. 226 notEmpty: {
  227. 227 message: 'The username is required and cannot be empty'
  228. 228 },
  229. 229 stringLength: {
  230. 230 min: 6,
  231. 231 max: 30,
  232. 232 message: 'The username must be more than 6 and less than 30 characters long'
  233. 233 },
  234. 234 regexp: {
  235. 235 regexp: /^[a-zA-Z0-9_\.]+$/,
  236. 236 message: 'The username can only consist of alphabetical, number, dot and underscore'
  237. 237 },
  238. 238 remote: {
  239. 239 url: 'remote.php',
  240. 240 message: 'The username is not available'
  241. 241 },
  242. 242 different: {
  243. 243 field: 'password',
  244. 244 message: 'The username and password cannot be the same as each other'
  245. 245 }
  246. 246 }
  247. 247 },
  248. 248 email: {
  249. 249 validators: {
  250. 250 emailAddress: {
  251. 251 message: 'The input is not a valid email address'
  252. 252 }
  253. 253 }
  254. 254 },
  255. 255 password: {
  256. 256 validators: {
  257. 257 notEmpty: {
  258. 258 message: 'The password is required and cannot be empty'
  259. 259 },
  260. 260 identical: {
  261. 261 field: 'confirmPassword',
  262. 262 message: 'The password and its confirm are not the same'
  263. 263 },
  264. 264 different: {
  265. 265 field: 'username',
  266. 266 message: 'The password cannot be the same as username'
  267. 267 }
  268. 268 }
  269. 269 },
  270. 270 confirmPassword: {
  271. 271 validators: {
  272. 272 notEmpty: {
  273. 273 message: 'The confirm password is required and cannot be empty'
  274. 274 },
  275. 275 identical: {
  276. 276 field: 'password',
  277. 277 message: 'The password and its confirm are not the same'
  278. 278 },
  279. 279 different: {
  280. 280 field: 'username',
  281. 281 message: 'The password cannot be the same as username'
  282. 282 }
  283. 283 }
  284. 284 },
  285. 285 birthday: {
  286. 286 validators: {
  287. 287 date: {
  288. 288 format: 'YYYY/MM/DD',
  289. 289 message: 'The birthday is not valid'
  290. 290 }
  291. 291 }
  292. 292 },
  293. 293 gender: {
  294. 294 validators: {
  295. 295 notEmpty: {
  296. 296 message: 'The gender is required'
  297. 297 }
  298. 298 }
  299. 299 },
  300. 300 'languages[]': {
  301. 301 validators: {
  302. 302 notEmpty: {
  303. 303 message: 'Please specify at least one language you can speak'
  304. 304 }
  305. 305 }
  306. 306 },
  307. 307 'programs[]': {
  308. 308 validators: {
  309. 309 choice: {
  310. 310 min: 2,
  311. 311 max: 4,
  312. 312 message: 'Please choose 2 - 4 programming languages you are good at'
  313. 313 }
  314. 314 }
  315. 315 },
  316. 316 captcha: {
  317. 317 validators: {
  318. 318 callback: {
  319. 319 message: 'Wrong answer',
  320. 320 callback: function(value, validator) {
  321. 321 var items = $('#captchaOperation').html().split(' '), sum = parseInt(items[0]) + parseInt(items[2]);
  322. 322 return value == sum;
  323. 323 }
  324. 324 }
  325. 325 }
  326. 326 }
  327. 327 }
  328. 328 });
  329. 329
  330. 330 // Validate the form manually
  331. 331 $('#validateBtn').click(function() {
  332. 332 $('#defaultForm').bootstrapValidator('validate');
  333. 333 });
  334. 334
  335. 335 $('#resetBtn').click(function() {
  336. 336 $('#defaultForm').data('bootstrapValidator').resetForm(true);
  337. 337 });
  338. 338 });
  339. 339 </script>
  340. 340 </body>
  341. 341 </html>

  看331行,点击提交时,用 

  1. $('#defaultForm').bootstrapValidator('validate'); 

触发表单验证

 

下面是碰到的一个坑:

bootstrapValidator默认逻辑是当表单验证失败时,把按钮给变灰色。
但是项目中,button并不在form内部,是通过事件绑定来ajax提交的。那么问题来了:

项目需要当form验证失败时,不执行所绑定的后续事件。百度半天找不到相关资料,最后还是要靠google:

 

  1. $("#yourform").submit(function(ev){ev.preventDefault();});
  2. $("#submit").on("click", function(){
  3.  
  4. var bootstrapValidator = $("#yourform").data('bootstrapValidator');
  5. bootstrapValidator.validate();
  6. if(bootstrapValidator.isValid())
  7. $("#yourform").submit();
  8. else return;
  9.  
  10. });

  

酱紫就可以判断表单验证是否通过了。

 

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号