Custom validator method not working

Custom validator method not working

I'm trying to add a few custom methods to the validator. It doesn't seem to be working properly.

I have an activity drop down. An activity has to be selected. If the value of the activity is 1, 2, or 3 a division and account have to be selected as well. Otherwise those fields can be empty.

I've tried to alert the values in my custom methods but it doesn't seem to fire all the time and even when the value is greater than 3 it doesn't clear the errors if they show up. I guess I'm thinking the validator fires on the value change in each of the fields of the form, but maybe it only fires on the submit function.

Here is the javascript I'm using:
  1. jQuery.validator.addMethod('activitySelected', function (value, element) {
  2. value = parseInt(value.replace(/\s+/g, ''));
  3. return this.optional(element) || value > 0;
  4. }, 'Please select an activity.');
  5. jQuery.validator.addMethod('accountSelected', function (value, element) {
  6. value = parseInt(value.replace(/\s+/g, ''));
  7. var activity = parseInt($('#activity').val());
  8. alert(activity);alert(this.optional(element));alert(((activity > 0 || activity < 4) && value > 0));
  9. return this.optional(element) || ;
  10. }, 'Please select an account.');
  11. jQuery.validator.addMethod('divisionSelected', function (value, element) {
  12. value = parseInt(value.replace(/\s+/g, ''));
  13. var activity = parseInt($('#activity').val());
  14. return this.optional(element) || ((activity > 0 || activity < 4) && value > 0);
  15. }, 'Please select a division.');

  16. $('#log_form').validate({
  17. rules: {
  18. time_start: {
  19. required: true
  20. },
  21. time_end: {
  22. required: true
  23. },
  24. activity: {
  25. activitySelected: true
  26. },
  27. account: {
  28. accountSelected: true
  29. },
  30. division: {
  31. divisionSelected: true
  32. }
  33. },
  34. highlight: function(el) {
  35. $(el).closest('.control-group').removeClass('success').addClass('error');
  36. },
  37. success: function(el) {
  38. $(el).closest('.control-group').removeClass('error');
  39. },
  40. submitHandler: function(form) {
  41. form.submit();
  42. }
  43. });