errorcontainer-demo.html 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Test for jQuery validate() plugin</title>
  6. <link rel="stylesheet" media="screen" href="css/screen.css">
  7. <script src="../lib/jquery.js"></script>
  8. <script src="../dist/jquery.validate.js"></script>
  9. <style>
  10. .cmxform fieldset p.error label {
  11. color: red;
  12. }
  13. div.container {
  14. background-color: #eee;
  15. border: 1px solid red;
  16. margin: 5px;
  17. padding: 5px;
  18. }
  19. div.container ol li {
  20. list-style-type: disc;
  21. margin-left: 20px;
  22. }
  23. div.container {
  24. display: none
  25. }
  26. .container label.error {
  27. display: inline;
  28. }
  29. form.cmxform {
  30. width: 30em;
  31. }
  32. form.cmxform label.error {
  33. display: block;
  34. margin-left: 1em;
  35. width: auto;
  36. }
  37. </style>
  38. <script>
  39. // only for demo purposes
  40. $.validator.setDefaults({
  41. submitHandler: function() {
  42. alert("submitted! (skipping validation for cancel button)");
  43. }
  44. });
  45. $().ready(function() {
  46. $("#form1").validate({
  47. errorLabelContainer: $("#form1 div.error")
  48. });
  49. var container = $('div.container');
  50. // validate the form when it is submitted
  51. var validator = $("#form2").validate({
  52. errorContainer: container,
  53. errorLabelContainer: $("ol", container),
  54. wrapper: 'li'
  55. });
  56. $(".cancel").click(function() {
  57. validator.resetForm();
  58. });
  59. });
  60. </script>
  61. </head>
  62. <body>
  63. <h1 id="banner"><a href="https://jqueryvalidation.org/">jQuery Validation Plugin</a> Demo</h1>
  64. <div id="main">
  65. <form method="get" class="cmxform" id="form1" action="">
  66. <fieldset>
  67. <legend>Login Form</legend>
  68. <p>
  69. <label>Username</label>
  70. <input name="user" title="Please enter your username (at least 3 characters)" required minlength="3">
  71. </p>
  72. <p>
  73. <label>Password</label>
  74. <input type="password" maxlength="12" name="password" title="Please enter your password, between 5 and 12 characters" required minlength="5">
  75. </p>
  76. <div class="error">
  77. </div>
  78. <p>
  79. <input class="submit" type="submit" value="Login">
  80. </p>
  81. </fieldset>
  82. </form>
  83. <!-- our error container -->
  84. <div class="container">
  85. <h4>There are serious errors in your form submission, please see below for details.</h4>
  86. <ol>
  87. <li>
  88. <label for="email" class="error">Please enter your email address</label>
  89. </li>
  90. <li>
  91. <label for="phone" class="error">Please enter your phone <b>number</b> (between 2 and 8 characters)</label>
  92. </li>
  93. <li>
  94. <label for="address" class="error">Please enter your address (at least 3 characters)</label>
  95. </li>
  96. <li>
  97. <label for="avatar" class="error">Please select an image (png, jpg, jpeg, gif)</label>
  98. </li>
  99. <li>
  100. <label for="cv" class="error">Please select a document (doc, docx, txt, pdf)</label>
  101. </li>
  102. </ol>
  103. </div>
  104. <form class="cmxform" id="form2" method="get" action="">
  105. <fieldset>
  106. <legend>Validating a complete form</legend>
  107. <p>
  108. <label for="email">Email</label>
  109. <input id="email" name="email" required type="email">
  110. </p>
  111. <p>
  112. <label for="agree">Favorite Color</label>
  113. <select id="color" name="color" title="Please select your favorite color!" required>
  114. <option></option>
  115. <option>Red</option>
  116. <option>Blue</option>
  117. <option>Yellow</option>
  118. </select>
  119. </p>
  120. <p>
  121. <label for="phone">Phone</label>
  122. <input id="phone" name="phone" required type="number" rangelength="[2,8]">
  123. </p>
  124. <p>
  125. <label for="address">Address</label>
  126. <input id="address" name="address" required minlength="3">
  127. </p>
  128. <p>
  129. <label for="avatar">Avatar</label>
  130. <input type="file" id="avatar" name="avatar" required>
  131. </p>
  132. <p>
  133. <label for="agree">Please agree to our policy</label>
  134. <input type="checkbox" class="checkbox" id="agree" title="Please agree to our policy!" name="agree" required>
  135. </p>
  136. <p>
  137. <input class="submit" type="submit" value="Submit">
  138. <input class="cancel" type="submit" value="Cancel">
  139. </p>
  140. </fieldset>
  141. </form>
  142. <div class="container">
  143. <h4>There are serious errors in your form submission, please see details above the form!</h4>
  144. </div>
  145. <a href="index.html">Back to main page</a>
  146. </div>
  147. </body>
  148. </html>