index.html 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>jQuery validation plug-in - main demo</title>
  6. <link rel="stylesheet" href="css/screen.css">
  7. <script src="../lib/jquery.js"></script>
  8. <script src="../dist/jquery.validate.js"></script>
  9. <script>
  10. $.validator.setDefaults({
  11. submitHandler: function() {
  12. alert("submitted!");
  13. }
  14. });
  15. $().ready(function() {
  16. // validate the comment form when it is submitted
  17. $("#commentForm").validate();
  18. // validate signup form on keyup and submit
  19. $("#signupForm").validate({
  20. rules: {
  21. firstname: "required",
  22. lastname: "required",
  23. username: {
  24. required: true,
  25. minlength: 2
  26. },
  27. password: {
  28. required: true,
  29. minlength: 5
  30. },
  31. confirm_password: {
  32. required: true,
  33. minlength: 5,
  34. equalTo: "#password"
  35. },
  36. email: {
  37. required: true,
  38. email: true
  39. },
  40. topic: {
  41. required: "#newsletter:checked",
  42. minlength: 2
  43. },
  44. agree: "required"
  45. },
  46. messages: {
  47. firstname: "Please enter your firstname",
  48. lastname: "Please enter your lastname",
  49. username: {
  50. required: "Please enter a username",
  51. minlength: "Your username must consist of at least 2 characters"
  52. },
  53. password: {
  54. required: "Please provide a password",
  55. minlength: "Your password must be at least 5 characters long"
  56. },
  57. confirm_password: {
  58. required: "Please provide a password",
  59. minlength: "Your password must be at least 5 characters long",
  60. equalTo: "Please enter the same password as above"
  61. },
  62. email: "Please enter a valid email address",
  63. agree: "Please accept our policy",
  64. topic: "Please select at least 2 topics"
  65. }
  66. });
  67. // propose username by combining first- and lastname
  68. $("#username").focus(function() {
  69. var firstname = $("#firstname").val();
  70. var lastname = $("#lastname").val();
  71. if (firstname && lastname && !this.value) {
  72. this.value = firstname + "." + lastname;
  73. }
  74. });
  75. //code to hide topic selection, disable for demo
  76. var newsletter = $("#newsletter");
  77. // newsletter topics are optional, hide at first
  78. var inital = newsletter.is(":checked");
  79. var topics = $("#newsletter_topics")[inital ? "removeClass" : "addClass"]("gray");
  80. var topicInputs = topics.find("input").attr("disabled", !inital);
  81. // show when newsletter is checked
  82. newsletter.click(function() {
  83. topics[this.checked ? "removeClass" : "addClass"]("gray");
  84. topicInputs.attr("disabled", !this.checked);
  85. });
  86. });
  87. </script>
  88. <style>
  89. #commentForm {
  90. width: 500px;
  91. }
  92. #commentForm label {
  93. width: 250px;
  94. }
  95. #commentForm label.error, #commentForm input.submit {
  96. margin-left: 253px;
  97. }
  98. #signupForm {
  99. width: 670px;
  100. }
  101. #signupForm label.error {
  102. margin-left: 10px;
  103. width: auto;
  104. display: inline;
  105. }
  106. #newsletter_topics label.error {
  107. display: none;
  108. margin-left: 103px;
  109. }
  110. </style>
  111. </head>
  112. <body>
  113. <h1 id="banner"><a href="https://jqueryvalidation.org/">jQuery Validation Plugin</a> Demo</h1>
  114. <div id="main">
  115. <p>Default submitHandler is set to display an alert into of submitting the form</p>
  116. <form class="cmxform" id="commentForm" method="get" action="">
  117. <fieldset>
  118. <legend>Please provide your name, email address (won't be published) and a comment</legend>
  119. <p>
  120. <label for="cname">Name (required, at least 2 characters)</label>
  121. <input id="cname" name="name" minlength="2" type="text" required>
  122. </p>
  123. <p>
  124. <label for="cemail">E-Mail (required)</label>
  125. <input id="cemail" type="email" name="email" required>
  126. </p>
  127. <p>
  128. <label for="curl">URL (optional)</label>
  129. <input id="curl" type="url" name="url">
  130. </p>
  131. <p>
  132. <label for="ccomment">Your comment (required)</label>
  133. <textarea id="ccomment" name="comment" required></textarea>
  134. </p>
  135. <p>
  136. <input class="submit" type="submit" value="Submit">
  137. </p>
  138. </fieldset>
  139. </form>
  140. <form class="cmxform" id="signupForm" method="get" action="">
  141. <fieldset>
  142. <legend>Validating a complete form</legend>
  143. <p>
  144. <label for="firstname">Firstname</label>
  145. <input id="firstname" name="firstname" type="text">
  146. </p>
  147. <p>
  148. <label for="lastname">Lastname</label>
  149. <input id="lastname" name="lastname" type="text">
  150. </p>
  151. <p>
  152. <label for="username">Username</label>
  153. <input id="username" name="username" type="text">
  154. </p>
  155. <p>
  156. <label for="password">Password</label>
  157. <input id="password" name="password" type="password">
  158. </p>
  159. <p>
  160. <label for="confirm_password">Confirm password</label>
  161. <input id="confirm_password" name="confirm_password" type="password">
  162. </p>
  163. <p>
  164. <label for="email">Email</label>
  165. <input id="email" name="email" type="email">
  166. </p>
  167. <p>
  168. <label for="agree">Please agree to our policy</label>
  169. <input type="checkbox" class="checkbox" id="agree" name="agree">
  170. </p>
  171. <p>
  172. <label for="newsletter">I'd like to receive the newsletter</label>
  173. <input type="checkbox" class="checkbox" id="newsletter" name="newsletter">
  174. </p>
  175. <fieldset id="newsletter_topics">
  176. <legend>Topics (select at least two) - note: would be hidden when newsletter isn't selected, but is visible here for the demo</legend>
  177. <label for="topic_marketflash">
  178. <input type="checkbox" id="topic_marketflash" value="marketflash" name="topic">Marketflash
  179. </label>
  180. <label for="topic_fuzz">
  181. <input type="checkbox" id="topic_fuzz" value="fuzz" name="topic">Latest fuzz
  182. </label>
  183. <label for="topic_digester">
  184. <input type="checkbox" id="topic_digester" value="digester" name="topic">Mailing list digester
  185. </label>
  186. <label for="topic" class="error">Please select at least two topics you'd like to receive.</label>
  187. </fieldset>
  188. <p>
  189. <input class="submit" type="submit" value="Submit">
  190. </p>
  191. </fieldset>
  192. </form>
  193. <h3>Synthetic examples</h3>
  194. <ul>
  195. <li><a href="errorcontainer-demo.html">Error message containers in action</a>
  196. </li>
  197. <li><a href="custom-messages-data-demo.html">Custom Messages as Element Data</a>
  198. </li>
  199. <li><a href="radio-checkbox-select-demo.html">Radio and checkbox buttons and selects</a>
  200. </li>
  201. <li><a href="ajaxSubmit-integration-demo.html">Integration with Form Plugin (AJAX submit)</a>
  202. </li>
  203. <li><a href="custom-methods-demo.html">Custom methods and message display.</a>
  204. </li>
  205. <li><a href="dynamic-totals.html">Dynamic forms</a>
  206. </li>
  207. <li><a href="themerollered.html">Forms styled with jQuery UI Themeroller</a>
  208. </li>
  209. <li><a href="tinymce/">TinyMCE3 Demo</a>
  210. </li>
  211. <li><a href="tinymce4/">TinyMCE4 Demo</a>
  212. </li>
  213. <li><a href="file_input.html">File inputs</a>
  214. </li>
  215. <li><a href="jquerymobile.html">jQuery Mobile Form Validation</a>
  216. </li>
  217. <li><a href="errors-within-labels.html">Displaying Errors within Field Labels</a>
  218. </li>
  219. <li><a href="requirejs/index.html">Loading via RequireJS</a>
  220. </li>
  221. <li><a href="bootstrap/index.html">Using with Bootstrap</a>
  222. </li>
  223. <li><a href="semantic-ui/index.html">Using with Semantic-UI</a>
  224. </li>
  225. </ul>
  226. <h3>Real-world examples</h3>
  227. <ul>
  228. <li><a href="cinema/">Retro Cinema Registration</a></li>
  229. <li><a href="milk/">Remember The Milk signup form</a></li>
  230. <li><a href="marketo/">Marketo signup form</a></li>
  231. <li><a href="multipart/">Buy and Sell a House multipart form</a></li>
  232. <li><a href="captcha/">Remote captcha validation</a></li>
  233. </ul>
  234. </div>
  235. </body>
  236. </html>