option-target.html 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Target</title>
  6. <link rel="stylesheet" href="demo.css">
  7. <script src="https://cdn.jsdelivr.net/jquery/1.12.3/jquery.min.js"></script>
  8. <script src="../dist/jquery.validator.js?local=en"></script>
  9. </head>
  10. <body>
  11. <form class="form" data-validator-option="{timely:2, theme:'yellow_right'}">
  12. <h3>Target to other input</h3>
  13. <div class="form-item">
  14. <label class="label">Name</label>
  15. <input type="text" class="input-sm" name="firstName" data-target="#lastName" data-rule="required" placeholder="First Name">
  16. <input type="text" class="input-sm" name="lastName" id="lastName" data-rule="required" placeholder="Last Name">
  17. </div>
  18. <div class="form-item">
  19. <button type="submit">Submit</button>
  20. </div>
  21. </form>
  22. <form class="form" data-validator-option="{timely:2, theme:'simple_right'}">
  23. <h3>Target to "msg-box"</h3>
  24. <div class="form-item">
  25. <input type="text" name="name" data-rule="Name:required" data-target="#holdMsg" placeholder="Name">
  26. </div>
  27. <div class="form-item">
  28. <button type="submit">Submit</button>
  29. <span class="msg-box" id="holdMsg"></span>
  30. </div>
  31. </form>
  32. <form class="form" data-validator-option="{timely:2}">
  33. <h3>Target to a container</h3>
  34. <div id="msgContainer"></div>
  35. <div class="form-item">
  36. <input type="text" name="name" data-rule="Name:required" data-target="#msgContainer">
  37. </div>
  38. <div class="form-item">
  39. <button type="submit">Submit</button>
  40. </div>
  41. </form>
  42. <form class="form" id="form4">
  43. <h3>Dynamic message place.</h3>
  44. <div class="form-item">
  45. <label class="label">Name</label>
  46. <input type="text" class="input-sm" name="firstName" data-rule="required" placeholder="First Name">
  47. <input type="text" class="input-sm" name="lastName" data-rule="required" placeholder="Last Name">
  48. </div>
  49. <div class="form-item">
  50. <button type="submit">Submit</button>
  51. </div>
  52. </form>
  53. <script>
  54. $('#form4').validator({
  55. timely:2,
  56. stopOnError: true,
  57. display: function(input) {
  58. return $(input).attr('placeholder');
  59. },
  60. target: function(input) {
  61. var $formitem = $(input).closest('.form-item'),
  62. $msgbox = $formitem.find('span.msg-box');
  63. if (!$msgbox.length) {
  64. $msgbox = $('<span class="msg-box"></span>').appendTo($formitem);
  65. }
  66. return $msgbox;
  67. }
  68. });
  69. </script>
  70. </body>
  71. </html>