custom-methods-demo.html 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. <script>
  10. // extend the current rules with new groovy ones
  11. // this one requires the text "buga", we define a default message, too
  12. $.validator.addMethod("buga", function(value) {
  13. return value == "buga";
  14. }, 'Please enter "buga"!');
  15. // this one requires the value to be the same as the first parameter
  16. $.validator.methods.equal = function(value, element, param) {
  17. return value == param;
  18. };
  19. $().ready(function() {
  20. var validator = $("#texttests").bind("invalid-form.validate", function() {
  21. $("#summary").html("Your form contains " + validator.numberOfInvalids() + " errors, see details below.");
  22. }).validate({
  23. debug: true,
  24. errorElement: "em",
  25. errorContainer: $("#warning, #summary"),
  26. errorPlacement: function(error, element) {
  27. error.appendTo(element.parent("td").next("td"));
  28. },
  29. success: function(label) {
  30. label.text("ok!").addClass("success");
  31. },
  32. rules: {
  33. number: {
  34. required: true,
  35. minlength: 3,
  36. maxlength: 15,
  37. number: true
  38. },
  39. secret: "buga",
  40. math: {
  41. equal: 11
  42. }
  43. }
  44. });
  45. });
  46. </script>
  47. <style>
  48. form.cmxform {
  49. width: 50em;
  50. }
  51. em.error {
  52. background:url("images/unchecked.gif") no-repeat 0px 0px;
  53. padding-left: 16px;
  54. }
  55. em.success {
  56. background:url("images/checked.gif") no-repeat 0px 0px;
  57. padding-left: 16px;
  58. }
  59. form.cmxform label.error {
  60. margin-left: auto;
  61. width: 250px;
  62. }
  63. em.error {
  64. color: black;
  65. }
  66. #warning {
  67. display: none;
  68. }
  69. </style>
  70. </head>
  71. <body>
  72. <h1 id="banner"><a href="https://jqueryvalidation.org/">jQuery Validation Plugin</a> Demo</h1>
  73. <div id="main">
  74. <form class="cmxform" id="texttests" method="get" action="foo.html">
  75. <h2 id="summary"></h2>
  76. <fieldset>
  77. <legend>Example with custom methods and heavily customized error display</legend>
  78. <table>
  79. <tr>
  80. <td>
  81. <label for="number">textarea</label>
  82. </td>
  83. <td>
  84. <input id="number" name="number" title="Please enter a number with at least 3 and max 15 characters!">
  85. </td>
  86. <td></td>
  87. </tr>
  88. <tr>
  89. <td>
  90. <label for="secret">Secret</label>
  91. </td>
  92. <td>
  93. <input name="secret" id="secret">
  94. </td>
  95. <td></td>
  96. </tr>
  97. <tr>
  98. <td>
  99. <label for="math">7 + 4 =</label>
  100. </td>
  101. <td>
  102. <input id="math" name="math" title="Please enter the correct result!">
  103. </td>
  104. <td></td>
  105. </tr>
  106. </table>
  107. <input class="submit" type="submit" value="Submit">
  108. </fieldset>
  109. </form>
  110. <h3 id="warning">Your form contains tons of errors! Please try again.</h3>
  111. <p><a href="index.html">Back to main page</a></p>
  112. </div>
  113. </body>
  114. </html>