bootstrapValidator + Ajax表单验证

前端之家收集整理的这篇文章主要介绍了bootstrapValidator + Ajax表单验证前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

首先,利用bootstrap Validator表单验证进行表单验证需要如下CSS和JS.

<link rel="stylesheet" type="text/css" href="css/bootstrap.css" />

<link rel="stylesheet" type="text/css" href="css/bootstrapValidator.css" />

<script src="js/jquery-1.10.2.min.js" type="text/javascript"></script>

<script src="js/bootstrap.js" type="text/javascript"></script>

<script src="js/bootstrapValidator.js"></script>

这些样式和Js可以百度下载,或者到bootstrap-validator的github上找https://github.com/1000hz/bootstrap-validator

其次,我们需要严格遵守相应的格式

代码示例如下:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>BootstrapValidator demo</title>
  5.  
  6. <link rel="stylesheet" href="../vendor/bootstrap/css/bootstrap.css"/>
  7. <link rel="stylesheet" href="../dist/css/bootstrapValidator.css"/>
  8.  
  9. <script type="text/javascript" src="../vendor/jquery/jquery-1.10.2.min.js"></script>
  10. <script type="text/javascript" src="../vendor/bootstrap/js/bootstrap.min.js"></script>
  11. <script type="text/javascript" src="../dist/js/bootstrapValidator.js"></script>
  12. </head>
  13. <body>
  14. <div class="container">
  15. <div class="row">
  16. <div class="page-header">
  17. <h1>Use error message that is returned from remote/callback validator</h1>
  18. </div>
  19.  
  20. <div class="col-lg-8 col-lg-offset-2">
  21. <form id="defaultForm" method="post" class="form-horizontal" action="target.PHP">
  22. <div class="form-group">
  23. <label class="col-lg-3 control-label">Username</label>
  24. <div class="col-lg-5">
  25. <input type="text" class="form-control" name="username" autocomplete="off" />
  26. </div>
  27. </div>
  28.  
  29. <div class="form-group">
  30. <label class="col-lg-3 control-label">Email address</label>
  31. <div class="col-lg-5">
  32. <input type="text" class="form-control" name="email" autocomplete="off" />
  33. </div>
  34. </div>
  35.  
  36. <div class="form-group">
  37. <label class="col-lg-3 control-label">Password</label>
  38. <div class="col-lg-5">
  39. <input type="password" class="form-control" name="password" />
  40. </div>
  41. </div>
  42.  
  43. <div class="form-group">
  44. <div class="col-lg-9 col-lg-offset-3">
  45. <button type="submit" class="btn btn-primary">Submit</button>
  46. </div>
  47. </div>
  48. </form>
  49. </div>
  50. </div>
  51.  
  52. <script type="text/javascript">
  53. $(document).ready(function() {
  54. $('#defaultForm').bootstrapValidator({
  55. message: 'This value is not valid',FeedbackIcons: {
  56. valid: 'glyphicon glyphicon-ok',invalid: 'glyphicon glyphicon-remove',validating: 'glyphicon glyphicon-refresh'
  57. },fields: {
  58. username: {
  59. message: 'The username is not valid',validators: {
  60. notEmpty: {
  61. message: 'The username is required and can\'t be empty'
  62. },remote: {
  63. url: 'remote2.PHP'
  64. },different: {
  65. field: 'password',message: 'The username and password can\'t be the same as each other'
  66. }
  67. }
  68. },email: {
  69. validators: {
  70. notEmpty: {
  71. message: 'The email address is required and can\'t be empty'
  72. },emailAddress: {
  73. message: 'The input is not a valid email address'
  74. },remote: {
  75. url: 'remote2.PHP'
  76. }
  77. }
  78. },password: {
  79. validators: {
  80. notEmpty: {
  81. message: 'The password is required and can\'t be empty'
  82. },different: {
  83. field: 'username',message: 'The password can\'t be the same as username'
  84. },callback: {
  85. callback: function(value,validator) {
  86. // Check the password strength
  87. if (value.length < 6) {
  88. return {
  89. valid: false,message: 'The password must be more than 6 characters'
  90. }
  91. }
  92.  
  93. if (value === value.toLowerCase()) {
  94. return {
  95. valid: false,message: 'The password must contain at least one upper case character'
  96. }
  97. }
  98. if (value === value.toUpperCase()) {
  99. return {
  100. valid: false,message: 'The password must contain at least one lower case character'
  101. }
  102. }
  103. if (value.search(/[0-9]/) < 0) {
  104. return {
  105. valid: false,message: 'The password must contain at least one digit'
  106. }
  107. }
  108.  
  109. return true;
  110. }
  111. }
  112. }
  113. }
  114. }
  115. });
  116. });
  117. </script>
  118. </body>
  119. </html>
当然这些代码在demo中都有。而我们需要修改为下面的ajax异步表单验证的形式,所以我们需要对原来代码进行修改
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>BootstrapValidator demo</title>
  5.  
  6. <link rel="stylesheet" href="../vendor/bootstrap/css/bootstrap.css"/>
  7. <link rel="stylesheet" href="../dist/css/bootstrapValidator.css"/>
  8.  
  9. <script type="text/javascript" src="../vendor/jquery/jquery-1.10.2.min.js"></script>
  10. <script type="text/javascript" src="../vendor/bootstrap/js/bootstrap.min.js"></script>
  11. <script type="text/javascript" src="../dist/js/bootstrapValidator.js"></script>
  12. </head>
  13. <body>
  14. <div class="container">
  15. <div class="row">
  16. <div class="page-header">
  17. <h1>Use error message that is returned from remote/callback validator</h1>
  18. </div>
  19.  
  20. <div class="col-lg-8 col-lg-offset-2">
  21. <form id="defaultForm" method="post" class="form-horizontal" action="target.PHP">
  22. <div class="form-group">
  23. <label class="col-lg-3 control-label">Username</label>
  24. <div class="col-lg-5">
  25. <input type="text" class="form-control" name="username" autocomplete="off" />
  26. </div>
  27. </div>
  28.  
  29. <div class="form-group">
  30. <label class="col-lg-3 control-label">Email address</label>
  31. <div class="col-lg-5">
  32. <input type="text" class="form-control" name="email" autocomplete="off" />
  33. </div>
  34. </div>
  35.  
  36. <div class="form-group">
  37. <label class="col-lg-3 control-label">Password</label>
  38. <div class="col-lg-5">
  39. <input type="password" class="form-control" name="password" />
  40. </div>
  41. </div>
  42.  
  43. <div class="form-group">
  44. <div class="col-lg-9 col-lg-offset-3">
  45. <button type="submit" class="btn btn-primary">Submit</button>
  46. </div>
  47. </div>
  48. </form>
  49. </div>
  50. </div>
  51.  
  52. <script type="text/javascript">
  53. $(document).ready(function() {
  54. $('#defaultForm').bootstrapValidator({
  55. message: 'This value is not valid',remote : {
  56. url : 'api/check_username',message : "该用户名已经存在!",delay : 500,type : 'post',},message: 'The password must contain at least one digit'
  57. }
  58. }
  59.  
  60. return true;
  61. }
  62. }
  63. }
  64. }
  65. }
  66. });
  67. });
  68. </script>
  69. </body>
  70. </html>
可以看到我们在第一个字段 username中的remote添加了ajax验证。delay表示的是ajax刷新的时间是0.5秒一次。

后台中,我们只需要返回一个true或一个false。

最好的参考: http://1000hz.github.io/bootstrap-validator/

猜你在找的Ajax相关文章