required-by-condition.html 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Required By Condition</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.min.js?local=en"></script>
  9. </head>
  10. <body>
  11. <form id="form1" class="form" data-validator-option="{timely:3,focusCleanup:true}">
  12. <h3>required( dependency-expression )</h3>
  13. <div class="form-item">
  14. <label for="isCompany"><input type="checkbox" id="isCompany" checked>Company</label>
  15. </div>
  16. <div class="form-item">
  17. <input type="text" name="companyName" data-rule="required(#isCompany:checked)">
  18. </div>
  19. <div class="form-item">
  20. <button type="submit">Submit</button>
  21. </div>
  22. </form>
  23. <form id="form2" class="form">
  24. <h3>required( dependency-callback )</h3>
  25. <div class="form-item">
  26. <label><input type="checkbox" id="isPersonal" checked>Personal</label>
  27. </div>
  28. <div class="form-item">
  29. <input type="text" name="email">
  30. </div>
  31. <div class="form-item">
  32. <button type="submit">Submit</button>
  33. </div>
  34. </form>
  35. <script>
  36. $('#form2').validator({
  37. timely: 3,
  38. focusCleanup: true,
  39. rules: {
  40. isPersonal: function() {
  41. return $('#isPersonal').prop('checked');
  42. }
  43. },
  44. fields: {
  45. email: 'required(isPersonal); email'
  46. }
  47. });
  48. </script>
  49. </body>
  50. </html>